PreparedStatement抛出语法错误 [英] PreparedStatement throws syntax error
本文介绍了PreparedStatement抛出语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PreparedStatements准备一个查询,当我用条件参数硬编码查询时它运行正常。
Im preparing a query using PreparedStatements and it runs fine when i hardcode te query with the condition parameter.
但如果参数是从setString传递的,则抛出错误( )方法。
but throws error , if the parameter is passed from setString() method.
com.mysql.jdbc.JDBC4PreparedStatement@2cf63e26: select * from linkedin_page_mess ages where company_id = '2414183' 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 '?' at line 1
在错误日志中,我的查询上面看起来很好。
In the error log, above my query looks fine.
public JSONObject getLinkedInMessages(String compId)
{
linlogger.log(Level.INFO, "Called getLinkedInMessages method");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
JSONObject resObj = new JSONObject();
JSONArray tempArray = new JSONArray();
try
{
conn = InitOrGetConnection();
String query = "select * from linkedin_page_messages where company_id = ?";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, compId);
System.out.println("\n\n"+pst.toString());
rs= pst.executeQuery(query);
// process resultset logics
}
catch(Exception e)
{
System.out.println(e);
linlogger.log(Level.INFO, "Exception occured "+e.toString());
}
}
PreparedStatements有什么问题吗?
Is there anything wrong with the PreparedStatements?
推荐答案
删除此行中的参数:
rs= pst.executeQuery(query);
必须
rs= pst.executeQuery();
因为语句是在 PreparedStatement pst = conn.prepareStatement(query)准备的;
execute(String sql)
继承自语句并将执行声明(sql)没有准备好。
execute(String sql)
is inherited from Statement and will execute the satement (sql) without prepared it.
这篇关于PreparedStatement抛出语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文