PreparedStatement 或 CallableStatement,方法不能带有参数

对于 PreparedStatement CallableStatement,方法不能带有参数

继承了Statement接口中所有方法的PreparedStatement接口都有自己的executeQueryexecuteUpdateexecute方法。Statement对象本身不包含SQL语句,因而必须给Statement.execute方法提供SQL语句作为参数。PreparedStatement对象并不将SQL语句作为参数提供给这些方法,因为它们已经包含预编译SQL语句。CallableStatement对象继承这些方法的PreparedStatement形式。对于这些方法的PreparedStatementCallableStatement版本,使用查询参数将抛出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();

你可能感兴趣的:(历史的尘埃)