I would to create a java program whereby there is a JTable(which fetches content from mysql database and one of its columns is boolean). There is only one button which when clicked, should pop up a message displaying which rows have been checked(the rows whose checkbox is checked). Here is my code;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
setTitle("Found Items");
try {
String myDriver = "com.mysql.cj.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/lostfound";
Class.forName(myDriver);
Connection c = DriverManager.getConnection(myUrl, "root", "");
String sql = "SELECT * FROM found";
Statement st = c.createStatement();
// execute the query, and get a java resultset
ResultSet rs =st.executeQuery(sql);
int row =0;
while(rs.next())
{
private static final int BOOLEAN_COLUMN = 4;
String name = rs.getString("name");
String description = rs.getString("description");
String location = rs.getString("location");
meza.getModel().setValueAt(name, row, 0);
meza.getModel().setValueAt(description, row, 1);
meza.getModel().setValueAt(location, row, 2);
row++;
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
meza.show();
claim.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i =0; i<meza.getRowCount(); i++)
{
Boolean checked = Boolean.valueOf(meza.getValueAt(i,3).toString());
String col = meza.getValueAt(i, 1).toString();
//DISPLAY POP UP
if(checked)
{
JOptionPane.showMessageDialog(null,col);
}
}
}
});
}
However, i am getting this error.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at NewJFrame$1.actionPerformed(NewJFrame.java:65)
Kindly assist
Don't declare your variable with private or public in your method :
private static final int BOOLEAN_COLUMN = 4; //this is forbidden
Instead use :
int BOOLEAN_COLUMN = 4;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.