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
LVL 1
liamrichardsAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Searching would be something like below:
TableModel model = table.getModel();
for(int row=0;row<model.getRowCount();row++) {
	for(int col=0;col<model.getColumnCount();col++) {
		Object currentField = model.getValueAt(row,col);
 
 
		if(currentField.toString().indexOf(searchString) > -1) {
			found = true;
			// Do something
		}
	}
}

Open in new window

0
 
liamrichardsAuthor Commented:
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

0
 
objectsCommented:
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

0
All Courses

From novice to tech pro — start learning today.