【发布时间】: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