Link to home
Start Free TrialLog in
Avatar of liamrichards
liamrichardsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Make fields assending or decending and also search function

I have a simple databse try and catch written by a freind. However i need advice as how to make slight changes...the first being how can i make it assending or decending by clicking one of the field subjects.

Also I would like to add a search function there where i can search by using either Project_Ref or Sales Engineer.

Any help would be greatly appreciated.

P.S. if you require more information, please feel free to ask.

Regards
Liam
untitled.JPG
Avatar of Mick Barry
Mick Barry
Flag of Australia image

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of liamrichards

ASKER

here my code , i dont think it got posted previously
package desktopdatabaseapplication;
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Liam Richards
 */
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
 
public class ViewDatabase extends JFrame {
 
    JTextField hostField;
    JTextField queryField;
    QueryTableModel qtm;
    JTable table;
 
    public ViewDatabase() {
        super("View,Edit and Delete Records of the database");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(1400, 600);
 
        qtm = new QueryTableModel();
 
        table = new JTable(qtm);
        table.setAutoscrolls(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollpane = new JScrollPane(table);
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 2));
        JButton editButton = new JButton("Edit");
        editButton.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                
                String project_ref = (String) qtm.getValueAt(row, 0);
                EditEntry editEntry = new EditEntry(project_ref);
                dispose();
                editEntry.setVisible(true);
                
            }
        });
        p1.add(editButton);
 
        JButton deleteButton = new JButton("Delete");
        deleteButton.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                
                String project_ref = (String) qtm.getValueAt(row, 0);
                System.out.println("Row:"+row+"value:"+project_ref);
                Connection connection = null;
                String url = "jdbc:odbc:commend";
                String username = "";
                String password = "";
 
                try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    connection = DriverManager.getConnection(url, username, password);
 
                    String query = "delete * from commend where project_ref = '" + project_ref +"';";
                    System.out.println("Query:" + query);
                    Statement stmnt = connection.createStatement();
                    boolean res = stmnt.execute(query);
                    JOptionPane.showMessageDialog(null, "The entry has been sucessfully Deleted.");
                    System.out.println("Successfull");
                    qtm.setQuery("select * from commend;");
        
                } catch (ClassNotFoundException cnfex) {
                    JOptionPane.showMessageDialog(null, "Driver Failed");
                    System.exit(1);
                } catch (SQLException sqlex) {
                    JOptionPane.showMessageDialog(null, "Unable To Connect");
                    sqlex.printStackTrace();
                }
            }
        });
        p1.add(deleteButton);
 
        getContentPane().add(p1, BorderLayout.SOUTH);
 
        qtm.setHostURL("jdbc:odbc:commend");
        qtm.setQuery("select * from commend;");
        getContentPane().add(scrollpane, BorderLayout.CENTER);
        
    }
 
    public static void main(String args[]) {
        ViewDatabase tt = new ViewDatabase();
        tt.setVisible(true);
    }
}

Open in new window

TableRowSorter was added in java6 if you don't need to support earlier versions

http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tables.html

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial