【问题标题】:Using variables in where clause of SQL statement in JAVA在JAVA中SQL语句的where子句中使用变量
【发布时间】:2014-02-16 18:37:47
【问题描述】:

我想根据存储在我的变量(supp_rec 数组)中的值从我的数据库中选择一些行。我在以前的类似帖子/问题上使用过提示。但是,我仍然收到一些错误消息。代码sn-p如下:

waiting_time = dbMngr.runQuery( "SELECT " + "ExpectedWaitingTime" + " FROM Student" +     "where deptID = '" + supp_rec[1] + "' and weight = '" + supp_rec[2] + "'");
prob = PoisonModel(Phase3GUI.existence_time -     Long.parseLong(waiting_time[0]),2,Phase3GUI.existence_time);
if (prob > 0.5)
 {
    dbMngr.execUpdate( "DELETE " + " FROM Student" + "where deptID NOT IN" + supp_rec[1] + " and weight NOT IN" + supp_rec[2]);
 }+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

错误信息如下:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'deptID NOT IN1 and weight NOT IN8' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1' and weight = '8'' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

对于如何解决这些错误,我将不胜感激。谢谢。

【问题讨论】:

  • 使用占位符。将代码更新为 first 然后再次询问。无论如何,如果您查看实际的 SQL 文本,您会立即看到几个语法问题。
  • 这是一种非常混乱的做事方式。而不是“SELECT”+ 变量“其余语句;” try String.format("SELECT %s rest of statement;", variable);
  • @735Tesla 但是将String.format 用于任意值是在此处处理整体任务的poor way
  • 另外我不知道你是否想要这种方式,但将 ExpectedWaitingTime 括在引号中使其解释字符串字母 ally 而不是查看变量的值。如果它不是一个变量,为什么不把它全部放在一个字符串中呢?
  • @735Tesla 老实说没关系。教授可以持续应用的良好技术。

标签: java mysql sql sqlexception where-in


【解决方案1】:

mysql的错误信息是

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 'deptID NOT 附近使用正确的语法 IN1 和权重 NOT IN8' 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 的第 1 行

因此,如果没有空格,mysql 会尝试将 IN1 解析为其语法的一部分,因此查询失败。它应该是 IN 1,其中 IN 是有效的 SQL 令牌。

要更正此问题,请在语句中的 IN 之后添加一个空格 -

dbMngr.execUpdate( "DELETE " + " FROM Student" + "where deptID NOT IN " + supp_rec[1] + " and weight NOT IN " + supp_rec[2]);

编辑:注意到您为 NOT IN 传递了一个值。在这种情况下,它应该只是 != 运算符。

dbMngr.execUpdate( "DELETE " + " FROM Student" + "where deptID != " + supp_rec[1] + " and weight != " + supp_rec[2]);

【讨论】:

  • 仍有错误。 (至少缺少一个空格,我怀疑 supp_rec[x] 作为 IN 运算符无效 - 特别是因为我怀疑它们缺少括号,但我不完全确定它们应该包含什么。)
【解决方案2】:

你真的应该使用 PreparedStatement。 SQL 将更容易阅读,并且您将避免大多数这些问题。

【讨论】:

  • 感谢大家的cmets。错误现已解决。
【解决方案3】:

在变量内容之间添加(),它们之间有空格并稍微格式化

dbMngr.execUpdate(String.format("DELETE FROM Student WHERE deptID NOT IN (%s) and weight NOT IN (%s)", supp_rec[1], supp_rec[2]));

另外,这对你来说风险很大,不要使用针对SQL Injections的保护措施

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2020-04-16
    • 1970-01-01
    • 2014-04-11
    • 2012-02-19
    • 2013-10-16
    相关资源
    最近更新 更多