我编写了以下代码,其中包含一个SQL查询,该查询获取了DB中特定商店的storeID:
private int inDB(String storename, String phone, int zipcode)
{
try
{
String select = "SELECT storeID FROM Store WHERE storename = ? OR phone = ? AND City_zipcode = ?;";
PreparedStatement pst = dbc.prepareStatement(select);
pst.setString(1, storename);
pst.setString(2, phone);
pst.setInt(3, zipcode);
ResultSet rs = pst.executeQuery(select);
while(rs.next())
{
return rs.getInt("storeID");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return -1;
}
不幸的是,这段代码引发了一个大规模的例外......我试图找出问题的一段时间。
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 '? OR phone = ? AND City_zipcode = ?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)
at db.Store.inDB(Store.java:297)
如您所见,我正在使用MySQL数据库驱动程序。所有其他几乎相同的方法都在起作用。它必须是导致问题的查询。但是当我尝试在phpMyAdmin中运行它时,它绝对没问题。
我希望你们中的某个人可以帮助我...
亲切的问候 //乔纳斯
答案 0 :(得分:3)
错误在这里改变它
ResultSet rs = pst.executeQuery(select);
以强>
ResultSet rs = pst.executeQuery();
您准备好将query
语句设置为preparedStatement
答案 1 :(得分:0)
我相信吗?是mysql的保留关键字...