大意导致Java访问DB2库时导出SQLCODE=-301, SQLSTATE=07006错误

本人在写一个java模块,目的是把数据插入到DB2库中,由于自己在建模时不知何时将表中一个字段由varchar修改成decimal,并且将此表重新建到开发库中,为此导致以前写好的功能模块报错,本人很纳闷,但又不知道什么原因,虽然知道可能是自己造成的,但具体原因却无论如何也想不起来,只能通过一个个可能的问题排查,最终找出是由于修改表的字段类型所致,以下,是建表语句、JAVA代码、和抛出的异常:

 


java代码:

            in = new FileInputStream(uploadedFile);
            pstm = con.prepareStatement("insert into TEST.YSB(XH,YSBNR,JGXH) values((select value(MAX(XH),0)+1 FROM ZJZX.ZJZX_SCYSB),?,?)",pstm.RETURN_GENERATED_KEYS);
            pstm.setBlob(1, getBlob(in));
       pstm.setNull(2, Types.VARCHAR);
            pstm.execute();


           

表结构如下:

create table TEST.YSB
(
   XH    DECIMAL(18)            not null      generated by default as identity,
   YSBNR BLOB(5242880),
   JGXH  DECIMAL(18),
);

 

以上软件运行时,会抛出如下异常:

 

com.ibm.db2.jcc.b.SqlException: DB2 SQL Error: SQLCODE=-301, SQLSTATE=07006, SQLERRMC=9, DRIVER=3.50.152
 at com.ibm.db2.jcc.b.wc.a(wc.java:55)
 at com.ibm.db2.jcc.b.wc.a(wc.java:126)
 at com.ibm.db2.jcc.b.tk.b(tk.java:1593)
 at com.ibm.db2.jcc.b.tk.c(tk.java:1576)
 at com.ibm.db2.jcc.t4.db.k(db.java:353)
 at com.ibm.db2.jcc.t4.db.a(db.java:59)
 at com.ibm.db2.jcc.t4.t.a(t.java:50)
 at com.ibm.db2.jcc.t4.tb.b(tb.java:200)
 at com.ibm.db2.jcc.b.uk.Gb(uk.java:2355)
 at com.ibm.db2.jcc.b.uk.e(uk.java:3129)
 at com.ibm.db2.jcc.b.uk.Bb(uk.java:1842)
 at com.ibm.db2.jcc.b.uk.execute(uk.java:1826)
 at com.pdcss.court.zjzx.servlet.UploadExcelServlet.saveZip(UploadExcelServlet.java:318)
 at com.pdcss.court.zjzx.servlet.UploadExcelServlet.saveYsb(UploadExcelServlet.java:294)
 at com.pdcss.court.zjzx.servlet.UploadExcelServlet.processRequest(UploadExcelServlet.java:94)
 at com.pdcss.court.zjzx.servlet.UploadExcelServlet.doPost(UploadExcelServlet.java:143)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
 at filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:124)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
 at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:834)
 at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
 at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
 at java.lang.Thread.run(Thread.java:619)

 

如果将上述java代码的第4行修改为

pstm.setNull(2, Types.DECIMAL);

就可以正确无误了,当然解决办法你也可以将表的字段类型修改回varchar。

你可能感兴趣的:(JAVA,java,db2,insert,table,null,sql)