先看如下代码:
使用上面的代码在不同数据库中分别出现错误如下:
(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),就不会设置参数,导致报错!
本文解析了在不同数据库(如Oracle、MySQL和SQL Server)中使用PreparedStatement时常见的错误及其原因。指出PreparedStatement创建时已缓存SQL语句,再次执行时未正确设置参数导致报错。
982

被折叠的 条评论
为什么被折叠?



