我试图将数据从jtable插入数据库!!从jtexfield插入前三列(stafftimetableid,staffname,staffid)(未发现错误,成功添加)但是当我试图从jtable插入时,它会提示java.null pointerExcetion错误!!
我在数据库连接中没有错误!!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jComboBox1.getSelectedItem().equals("Staff Time Table"))
{
try
{
PreparedStatement pst =null;
Connection con = clerkpanell.DBConnection.connectDB();
String data=jTable2.getValueAt(0,1).toString();
String sql = "insert into stafftimetable (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";
pst=con.prepareStatement(sql);
pst.executeUpdate();
// JOptionPane.showMessageDialog(null,"Added");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
答案 0 :(得分:1)
在此声明中String sql = "insert into stafftimetable (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";
请将ttid.getText()
,staffname.getText()
,staffid.getText()
存储到单独的变量中。像这样的东西,
String ttid=ttid.getText();
String staffname = staffname.getText();
String staffid = staffid.getText();
然后插入语句应该是这样的
String sql = "insert into stafftimetable (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.+"','"+staffname+"','"+staffid+"','"+data+"');";