通过JDBC方式连接DB2数据库时,url可写成如下格式:
XXXdb.connection.url=jdbc:db2://192.168.1.1:50000/xxxDB:deferPrepares=false;currentSchema=XXX
其中deferPrepares=false有何作用?
IBM官方对此属性的解释如下:deferPreparesSpecifies whether to defer prepare operations until statement execution. The data type of this property is boolean. The default is
true
for IBM DB2 Driver for JDBC and SQLJ type 4 connectivity. The property is not applicable to IBM DB2 Driver for JDBC and SQLJ type 2 connectivity.
Deferring prepare operations can reduce network delays. However, if you defer prepare operations, you need to ensure that input data types match DB2 table column types.
从上可以看出,如果不加deferPrepares=false,deferPrepares将默认为true,SQL将延迟准备操作。
如果设置为延迟准备,程序中使用PreparedStatement就需要指定对象的的类型和表字段的类型匹配,即便这样也可能出现问题。比如SQLCODE -517的错误。
com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -517, SQLSTATE: 07005, SQLERRMC: SQL_CURSH200C1
at com.ibm.db2.jcc.c.fg.e(fg.java:1596)
at com.ibm.db2.jcc.c.fg.a(fg.java:1225)
at com.ibm.db2.jcc.b.gb.n(gb.java:718)
at com.ibm.db2.jcc.b.gb.i(gb.java:255)
at com.ibm.db2.jcc.b.gb.c(gb.java:53)
at com.ibm.db2.jcc.b.w.c(w.java:46)
at com.ibm.db2.jcc.b.vb.h(vb.java:151)
at com.ibm.db2.jcc.c.fg.p(fg.java:1220)
at com.ibm.db2.jcc.c.gg.d(gg.java:2316)
at com.ibm.db2.jcc.c.gg.W(gg.java:457)
at com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440)