我正在使用SQlite作为数据库,并使用Java进行编码,下面提供了newID();
,updateTable();
和updateData();
函数
public void newID(){
String sql = "select count(*) ID from CashierDetails";
try{
pst= conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
int id = ((Number) rs.getObject(1)).intValue() + 1;
jTextField1.setText("" + id);
pst.close();
rs.close();
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public void updateTable()
{
String sql = "select * from CashierDetails";
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable5.setModel(DbUtils.resultSetToTableModel(rs));
pst.close();
rs.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
public void updateData ()
{
String sql = " update CashierDetails set "
+ " CashierName = '"+jTextField2.getText()+"',"
+ " SecretQ = '"+jComboBox1.getSelectedItem()+"',"
+ " Answer = '"+jTextField18.getText()+"',"
+ " MobileNo = '"+jTextField3.getText()+"',"
+ " Address = '"+jTextField5.getText()+"',"
+ " Email = '"+jTextField4.getText()+"',"
+ " Password = '"+jPasswordField1.getText()+"',"
+ " ConfirmPass = '"+jPasswordField2.getText()+"'"
+ " where CashierID = "+jTextField1.getText();
String sql2 = "update CashierAccount set "
+ " Username = '"+jTextField2.getText()+"',"
+ " SecretQ = '"+jComboBox1.getSelectedItem()+"',"
+ " Password = '"+jPasswordField1.getText()+"',"
+ " ConfirmPassword = '"+jPasswordField2.getText()+"',"
+ " Answer = '"+jTextField18.getText()+"'"
+ " where CashierID = "+jTextField1.getText();
try
{
pst = conn.prepareStatement(sql);
pst.execute();
pst.close();
rs.close();
pst = conn.prepareStatement (sql2);
pst.execute ();
JOptionPane.showMessageDialog(null, "Cashier Details have been updated");
pst.close();
rs.close();
updateTable();
newID();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
该函数没有错误,也不会引发任何异常,它显示一个对话框,提示“收银员详细信息已更新”,但是在SQLite CashierDetails表中,CashierAccount表中的细节均未更新,以下是SqLite表 The CashierDetails Table The CashierAccount Table
现在,如果我要通过SQLite Studio填充表(即从SQLite Studio创建行并输入数据),则可以通过updateData();
函数来更新该特定行
答案 0 :(得分:0)
致电pst.executeUpdate()
而不是pst.execute()
答案 1 :(得分:0)
显然,关闭准备好的语句是一个问题。以下代码可以正常运行,并根据需要更新列表
public void updateData ()
{
String sql = " update CashierDetails set "
+ " CashierName = '"+jTextField2.getText()+"',"
+ " SecretQ = '"+jComboBox1.getSelectedItem()+"',"
+ " Answer = '"+jTextField18.getText()+"',"
+ " MobileNo = '"+jTextField3.getText()+"',"
+ " Address = '"+jTextField5.getText()+"',"
+ " Email = '"+jTextField4.getText()+"',"
+ " Password = '"+jPasswordField1.getText()+"',"
+ " ConfirmPass = '"+jPasswordField2.getText()+"'"
+ " where CashierID = "+jTextField1.getText();
String sql2 = "update CashierAccount set "
+ " Username = '"+jTextField2.getText()+"',"
+ " SecretQ = '"+jComboBox1.getSelectedItem()+"',"
+ " Password = '"+jPasswordField1.getText()+"',"
+ " ConfirmPassword = '"+jPasswordField2.getText()+"',"
+ " Answer = '"+jTextField18.getText()+"'"
+ " where CashierID = "+jTextField1.getText();
try
{
pst = conn.prepareStatement(sql);
pst.executeUpdate();
pst.close();
updateTable();
pst = conn.prepareStatement (sql2);
pst.executeUpdate();
updateTable();
JOptionPane.showMessageDialog(null, "Cashier Details have been updated");
pst.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}