-2

i have created jtable but it doesn't show the last column name i don't know what i did wrong in code database have 4 columns id , name, fathername and phone number but jtable only show 3 columns.

public void load() {
    try {
        DBO db = new DBO();
        con = db.connect();
        String sql = "Select * from personinfo";
        PreparedStatement pst = con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        DefaultTableModel tb = new DefaultTableModel();
        Vector col = new Vector();
        for (int i = 1; i < count; i++) {
            col.addElement(rsmd.getColumnName(i));
        }   
        tb.setColumnIdentifiers(col);
        while (rs.next()) {
           Vector rows = new Vector();
            for (int j = 1; j < rsmd.getColumnCount(); j++) {
                rows.addElement(rs.getString(j));
            }                
            tb.addRow(rows);
            PersonTable.setModel(tb);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }

}

2 Answers2

0

I think you should use

pst.setString(1, "%"+name.getText()+"%");
Harshit
  • 5,147
  • 9
  • 46
  • 93
0

when creating a prepared statement ? is used to replace bind variables. Values which you are not using as bind variables cannot be used the way you want, in this case using like. You can most probably go through this PreparedStatement IN clause alternatives?

Your question is quiet easily a duplicate Using “like” wildcard in prepared statement

Community
  • 1
  • 1
Acewin
  • 1,657
  • 4
  • 17
  • 36