先看如下代码:
使用上面的代码在不同数据库中分别出现错误如下:
(1)Oracle
执行查询:ORA-03115: unsupported network datatype or representation
执行更新:ORA-01008: not all variables bound
(2)MySQL
(3)SQLServer
原因:PreparedStatement在创建时已经缓存了sql语句,以便后面设置参数,所以你在执行查询或更新时在附加sql语句,像这样:pstmt.executeQuery(sql),就不会设置参数,导致报错!
PreparedStatement pstmt = conn.prepareStatement(sql);
//执行查询
pstmt.executeQuery(sql);
//执行更新
pstmt.executeUpdate(sql);
使用上面的代码在不同数据库中分别出现错误如下:
(1)Oracle
执行查询:ORA-03115: unsupported network datatype or representation
执行更新:ORA-01008: not all variables bound
(2)MySQL
MySQL Exception: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
(3)SQLServer
SQLServer 2000 Exception: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid call Statement method: {0}
原因:PreparedStatement在创建时已经缓存了sql语句,以便后面设置参数,所以你在执行查询或更新时在附加sql语句,像这样:pstmt.executeQuery(sql),就不会设置参数,导致报错!