Hello Everyone, I`m New to java. I need to search a jtable when i`m typing in a jtext field.As an example if i`m serchin my jtable by customer id and in j table it has customer id`s like 10021 and 10022 when i have typed"1002" the jtable should show both values and when i have typed "10022" it should show only one result.I have writed the following code in side keypressed event of the jtextbox.But it is not working. please help me.thank you.
private void srchKeyPressed(java.awt.event.KeyEvent evt) {
int R=0;
DefaultTableModel dm = (DefaultTableModel)jTable1.getModel();
dm.rowsRemoved(null);
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost/users?" +
"user=root&password=root";
Connection con = DriverManager.getConnection(connectionUrl);
Statement stmt = null;
ResultSet rs = null;
//SQL query command
String SQL = "select * from customer where cusid='"+10021+"'";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()){
String s = rs.getString(1);
jTable1.setValueAt(s, R, 0);
s = rs.getString(2);
jTable1.setValueAt(s, R, 1);
s = rs.getString(3);
jTable1.setValueAt(s, R, 2);
s = rs.getString(4);
jTable1.setValueAt(s, R, 3);
s = rs.getString(5);
jTable1.setValueAt(s, R, 4);
s = rs.getString(6);
jTable1.setValueAt(s, R, 5);
s = rs.getString(7);
jTable1.setValueAt(s, R, 6);
s = rs.getString(8);
jTable1.setValueAt(s, R, 7);
R++;
}
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}