【发布时间】:2018-04-12 11:42:46
【问题描述】:
我无法使用 where 子句在 JDBC 上执行更新查询。我想通过连接一些变量来执行查询。
protected void updateEmail(String Email, String Username) {
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/project", "root", "");
String query = "update access set email=" + Email + " where username=" + Username;
Statement st = conn.createStatement();
st.executeUpdate(query);
} catch (SQLException ex) {
System.out.println(ex);
} finally {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
上面写着:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bhusal1' in 'where clause'
【问题讨论】:
-
这毫无意义。您的查询中没有该列名。那是你作为参数传递的值吗?
-
我确实有用户名为 bhusal1 的 coloum。
-
"update access set email="+Email+" where username=" ... 在此查询中,您将电子邮件和用户名作为列名,而不是 buhsal1。但 YCF_L 是对的。
-
@YCF_L 提供的答案是一种更好的方法。但是,您的代码的问题只是您忘记了引号...
String query = "update access set email='" + Email + "' where username='" + Username + "'"; -
我也试过了。字符串查询=“更新访问集电子邮件='”+电子邮件+“'其中用户名='”+用户名+“'”;