PrepareStatement的两个executeQuery语句
PrepareStatement有两种executeQuery方法,一种是带参的,一种是不带参的。
带参的executeQuery:
ResultSet executeQuery(String sql) throws SQLException
Executes the given SQL statement, which returns a single
ResultSet object.
Note:This method cannot be called on a PreparedStatement or CallableStatement.
- Parameters:
sql- an SQL statement to be sent to the database, typically a static SQLSELECTstatement- Returns:
- a
ResultSetobject that contains the data produced by the given query; nevernull - Throws:
SQLException- if a database access error occurs, this method is called on a closedStatement, the given SQL statement produces anything other than a singleResultSetobject, the method is called on aPreparedStatementorCallableStatementSQLTimeoutException- when the driver has determined that the timeout value that was specified by thesetQueryTimeoutmethod has been exceeded and has at least attempted to cancel the currently runningStatement
也就是说如果使用executeQuery(sql)这个方法的话,参数中的sql语句必须是静态的。也就是不能有“?”在语句中进行参数替换。
不带参的executeQuery:
ResultSet executeQuery() throws SQLException
Executes the SQL query in this
PreparedStatement object and returns the ResultSet object generated by the query.
- Returns:
- a
ResultSetobject that contains the data produced by the query; nevernull - Throws:
SQLException- if a database access error occurs; this method is called on a closedPreparedStatementor the SQL statement does not return aResultSetobjectSQLTimeoutException- when the driver has determined that the timeout value that was specified by thesetQueryTimeoutmethod has been exceeded and has at least attempted to cancel the currently runningStatement
浙公网安备 33010602011771号