我的程序有问题。请帮帮我。
java.sql.SQLException:无法使用executeQuery()发出数据操作语句。
Class.forName("com.mysql.jdbc.Driver");
String path = "jdbc:mysql://localhost:3306/sampledb";
String Username = "root";
String Password = "";
Connection con = DriverManager.getConnection(path, Username, Password);
Statement s = con.createStatement();
String rGanTz = "UPDATE info SET Firstname = '"+txt_Firstname.getText()+"', Lastname = '"+txt_Lastname.getText()+"', Contact = '"+txt_Contact.getText()+"', WHERE '"+txt_Edpno.getText()+"'=EDPNO";
s.executeQuery(rGanTz);
JOptionPane.showMessageDialog(null,"Data has been successfully Updated","Update file", JOptionPane.INFORMATION_MESSAGE,null);
答案 0 :(得分:2)
你应该考虑@OllieJones在关于使用预准备语句的评论中所说的内容。 @Rimas已经为您提供了解决方案,所以我只想提供一个例子:
Connection con = DriverManager.getConnection(path, Username, Password);
String rGanTz = "UPDATE info SET Firstname = ?, Lastname = ?, Contact = ? " +
"WHERE EDPNO = ?";
PreparedStatement ps = con.prepareStatement(rGanTz);
ps.setString(1, txt_Firstname.getText());
ps.setString(2, txt_Lastname.getText());
ps.setString(3, txt_Contact.getText());
ps.setString(4, txt_Edpno.getText());
ps.executeUpdate();