通过Netbeans Jform进行Mysql更新查询



我试图在netbeans上运行以下代码,但它抛出了这个错误"您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解在"?"附近使用的正确语法?,esalary=?,eage=?,egender=?,edept=?其中eid=?'在第1行";。请帮我处理错误

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
// TODO add your handling code here:
try{
//open connection
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","BakerStreet@221b");


//mysql query to update
String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
PreparedStatement ptsmt = con.prepareStatement(sql);
ptsmt.executeUpdate(sql);
ptsmt.setString(1,empName.getText());
ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
ptsmt.setString(4,empGen.getText());
ptsmt.setString(5,empDep.getText());
ptsmt.setInt(6,Integer.parseInt(id.getText()));



ptsmt.executeUpdate();


JOptionPane.showMessageDialog(this, "Record updated Successfully");

con.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}     

  1. 您已经调用了两次executeUpdate(sql(,因此删除一次
  2. 在向参数添加值之前,您已经调用了executeUpdate(sql(,因此请执行以下操作:

private void btUnUpdateActionPerformed(java.awt.event.ActionEvent evt({
//TODO在此处添加您的处理代码:尝试{//开路连接Class.forName("com.mysql.jdbc.Driver"(;Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false"quot;根"BakerStreet@221b"(;

//mysql query to update
String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
PreparedStatement ptsmt = con.prepareStatement(sql);

ptsmt.setString(1,empName.getText());
ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
ptsmt.setString(4,empGen.getText());
ptsmt.setString(5,empDep.getText());
ptsmt.setInt(6,Integer.parseInt(id.getText()));


//execute update ...
ptsmt.executeUpdate();


JOptionPane.showMessageDialog(this, "Record updated Successfully");

con.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}     

相关内容

  • 没有找到相关文章

最新更新