PreparedStatement 抛出语法错误 [英] PreparedStatement throws syntax error
本文介绍了PreparedStatement 抛出语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 PreparedStatements 准备一个查询,当我使用条件参数对 te 查询进行硬编码时,它运行良好.
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("
"+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屋!
查看全文