代码:在参数中准备语句?
java.sql.PreparedStatement pstmt=null;
String sql = "UPDATE Person SET address = ? WHERE name = ? ";
pstmt=thecon.prepareStatement(sql);
String addVar= JOptionPane.showInputDialog("Enter the new address");
String nameVar=JOptionPane.showInputDialog("Enter the Name");
pstmt.setString(1 , addVar);
pstmt.setString(2, nameVar);
// Tell affected no of rows in table
int num = pstmt.executeUpdate(sql);
//Step 7: Process the results of the query
System.out.println(num + " records updated");
thecon.close();
} catch (SQLException e) {
System.out.println("sql problem: "+e);
}
例外:
SQL问题:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在''附近使用正确的语法'? WHERE name =?'在第1行
有问题吗? – philshem
什么是SQL语法的问题? –
您正在使用错误的'executeUpdate'方法,您应该使用没有字符串参数的方法。 –