本文主要是介绍PreparedStatement 或 CallableStatement,方法不能带有参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对于 PreparedStatement 或 CallableStatement,方法不能带有参数
继承了Statement接口中所有方法的PreparedStatement接口都有自己的executeQuery,executeUpdate和execute方法。Statement对象本身不包含SQL语句,因而必须给Statement.execute方法提供SQL语句作为参数。PreparedStatement对象并不将SQL语句作为参数提供给这些方法,因为它们已经包含预编译SQL语句。CallableStatement对象继承这些方法的PreparedStatement形式。对于这些方法的PreparedStatement或CallableStatement版本,使用查询参数将抛出SQLException。
Wrong codes
String query = "select ENEWSLETTERSUBSCRIPTIONID, USERNAME, EMAILADDRESS, COUNTRYOFRESIDENCE, STATUS, CREATEDATE from ASS1EMAILSUBSCRIPTIONS where CREATEDATE between ? and ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setDate(1, startDt);
stmt.setDate(2, endDt);
ResultSet uprs = stmt.executeQuery(query);
Correct codes
String query = "select ENEWSLETTERSUBSCRIPTIONID, USERNAME, EMAILADDRESS, COUNTRYOFRESIDENCE, STATUS, CREATEDATE from ASS1EMAILSUBSCRIPTIONS where CREATEDATE between ? and ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setDate(1, startDt);
stmt.setDate(2, endDt);
ResultSet uprs = stmt.executeQuery();
这篇关于PreparedStatement 或 CallableStatement,方法不能带有参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!