获取通过JBDC在IBM DB2 V6R1(AS400)上插入的行数

ant*_*abo 7 java db2 spring jdbc jt400

我们最近迁移到AS400上较新的V6R1版本的DB2,我们使用Spring框架(v.2.5.6.)与数据库进行通信.我们正在调用Spring的NamedParameterJdbcTemplate.update()方法来插入新行,这个方法应该返回插入行的数量,没有发生的事情(我们得到的结果为零)虽然定期插入行.

我们得出结论,如果insert语句中没有主键列一切正常,那么当PK列是自动增量时没有问题,但在某些情况下我们必须插入PK值然后我们必须以某种方式处理经常插入行的情况没有注册JDBC或Spring.

有人可以帮忙吗?

dav*_*orp 4

更新:问题已在JTOpen 7.6中修复

\n\n
\n\n

我和 Ante 一起工作......这个问题与 Spring 无关,这是肯定的,因为我已经设置了一个使用普通 JDBC 和PreparedStatements 的项目。

\n\n

所以我将添加一些如何模拟问题的更多信息:

\n\n

数据定义语言:

\n\n
CREATE TABLE TEST_V6R1 (\n        ID INTEGER NOT NULL DEFAULT,\n        VALUE VARCHAR(50)\n    );\n\nALTER TABLE TEST_V6R1 ADD CONSTRAINT TEST_V6R1_PK PRIMARY KEY\n    (ID);\n
Run Code Online (Sandbox Code Playgroud)\n\n

SQL:

\n\n
insert into test_v6r1 (id, value ) values (?, ?)\n
Run Code Online (Sandbox Code Playgroud)\n\n

Java代码:

\n\n
public class TestV6R1 {\n\n   public static void main( String[] args ) {\n      Connection conn = null;\n      PreparedStatement ps1 = null;\n      PreparedStatement ps2 = null;\n      try {\n         conn = getNewConnection();\n         conn.setAutoCommit( false );\n\n         String value = "Test: " + new Timestamp( System.currentTimeMillis() );\n\n         // First statement which uses RETURN_GENERATED_KEYS\n         ps1 = conn.prepareStatement( "insert into test_v6r1 (id, value ) values (?, ?)", Statement.RETURN_GENERATED_KEYS );\n         ps1.setInt( 1, 1 );\n         ps1.setString( 2, value );\n         int ps1Rows = ps1.executeUpdate();\n         // in case of V5R4\n         // ps1Rows is 1\n         // in case of V6R1\n         // ps1Rows is 0\n\n         ResultSet ps1keys = ps1.getGeneratedKeys();\n         int ps1KeySize = 0;\n         if (ps1keys != null) {\n            ps1keys.last();\n            ps1KeySize = ps1keys.getRow();\n         }\n         System.out.println("PS1 - SQL insert affected " + ps1Rows + " rows and returned " + ps1KeySize + " keys");\n         System.out.println("PS1 - getUpdateCount()="+ ps1.getUpdateCount());\n\n         // Second statement which uses NO_GENERATED_KEYS\n         ps2 = conn.prepareStatement( "insert into test_v6r1 (id, value) values (?, ?)", Statement.NO_GENERATED_KEYS );\n         ps2.setInt( 1, 2 );\n         ps2.setString( 2, value );\n         int ps2Rows = ps2.executeUpdate();\n         // in case of V5R4\n         // ps2Rows is 1\n         // in case of V6R1\n         // ps2Rows is 1\n\n         ResultSet ps2Keys = ps2.getGeneratedKeys();\n         int ps2KeySize = 0;\n         if (ps2Keys != null) {\n            ps2Keys.last();\n            ps2KeySize = ps2Keys.getRow();\n         }\n\n         System.out.println("PS2 - SQL insert affected " + ps2Rows + " rows and returned " + ps2KeySize + " keys");\n         System.out.println("PS2 - getUpdateCount()="+ ps2.getUpdateCount());\n\n         conn.commit();\n      }\n      catch ( Throwable e ) {\n\n         e.printStackTrace();\n         try {\n            conn.rollback();\n         }\n         catch ( SQLException e1 ) {\n            e1.printStackTrace();\n         }\n      }\n      finally {\n         try {\n            if (ps1!=null) ps1.close();\n            if (ps2!=null) ps2.close();\n            if (conn!=null) conn.close();\n         }\n         catch ( SQLException e ) {\n            e.printStackTrace();\n         }\n      }\n\n   }\n\n   public static Connection getNewConnection() {\n      try {\n         Class.forName( "com.ibm.as400.access.AS400JDBCDriver" ); // Or any other driver\n      }\n      catch ( Exception x ) {\n         System.out.println( "Unable to load the driver class!" );\n      }\n      Connection dbConnection = null;\n      try {\n         // TEST - V6R1\n         dbConnection = DriverManager\n               .getConnection( "jdbc:as400://testServer;libraries=*libl;naming=system;sort=language;sort language=HRV;sort weight=shared;prompt=false;trace=true",\n                               "username",\n                               "password" );\n         // PRODUCTION - V5R4\n//         dbConnection = DriverManager\n//         .getConnection( "jdbc:as400://productionServer;libraries=*libl;naming=system;sort=language;sort language=HRV;sort weight=shared;prompt=false;trace=true",\n//                         "username",\n//                         "password" );\n\n      }\n      catch ( SQLException x ) {\n         System.out.println( "Couldn\xe2\x80\x99t get connection!" );\n      }\n\n      return dbConnection;\n   }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

V5R4控制台输出:

\n\n
Toolbox for Java - Open Source Software, JTOpen 7.3, codebase 5770-SS1 V7R1M0.03 2011/01/14 @B5\nas400: Properties  (12122347) : access = "all".\nas400: Properties  (12122347) : block size = "32".\nas400: Properties  (12122347) : block criteria = "2".\nas400: Properties  (12122347) : date format = "".\nas400: Properties  (12122347) : date separator = "".\nas400: Properties  (12122347) : decimal separator = "".\nas400: Properties  (12122347) : errors = "basic".\nas400: Properties  (12122347) : extended dynamic = "false".\nas400: Properties  (12122347) : libraries = "*libl".\nas400: Properties  (12122347) : naming = "system".\nas400: Properties  (12122347) : package = "".\nas400: Properties  (12122347) : package add = "true".\nas400: Properties  (12122347) : package cache = "false".\nas400: Properties  (12122347) : package clear = "false".\nas400: Properties  (12122347) : package error = "warning".\nas400: Properties  (12122347) : package library = "".\nas400: Properties  (12122347) : password = "".\nas400: Properties  (12122347) : prefetch = "true".\nas400: Properties  (12122347) : prompt = "false".\nas400: Properties  (12122347) : remarks = "system".\nas400: Properties  (12122347) : sort = "language".\nas400: Properties  (12122347) : sort language = "HRV".\nas400: Properties  (12122347) : sort table = "".\nas400: Properties  (12122347) : sort weight = "shared".\nas400: Properties  (12122347) : time format = "".\nas400: Properties  (12122347) : time separator = "".\nas400: Properties  (12122347) : trace = "true".\nas400: Properties  (12122347) : transaction isolation = "read uncommitted".\nas400: Properties  (12122347) : translate binary = "false".\nas400: Properties  (12122347) : user = "username".\nas400: Properties  (12122347) : package criteria = "default".\nas400: Properties  (12122347) : lob threshold = "32768".\nas400: Properties  (12122347) : secure = "false".\nas400: Properties  (12122347) : data truncation = "true".\nas400: Properties  (12122347) : proxy server = "".\nas400: Properties  (12122347) : secondary URL = "".\nas400: Properties  (12122347) : data compression = "true".\nas400: Properties  (12122347) : big decimal = "true".\nas400: Properties  (12122347) : thread used = "true".\nas400: Properties  (12122347) : cursor hold = "true".\nas400: Properties  (12122347) : lazy close = "false".\nas400: Properties  (12122347) : driver = "toolbox".\nas400: Properties  (12122347) : bidi string type = "5".\nas400: Properties  (12122347) : key ring name = "".\nas400: Properties  (12122347) : key ring password = "".\nas400: Properties  (12122347) : full open = "false".\nas400: Properties  (12122347) : server trace = "0".\nas400: Properties  (12122347) : database name = "".\nas400: Properties  (12122347) : extended metadata = "false".\nas400: Properties  (12122347) : cursor sensitivity = "asensitive".\nas400: Properties  (12122347) : behavior override = "0".\nas400: Properties  (12122347) : package ccsid = "13488".\nas400: Properties  (12122347) : minimum divide scale = "0".\nas400: Properties  (12122347) : maximum precision = "31".\nas400: Properties  (12122347) : maximum scale = "31".\nas400: Properties  (12122347) : translate hex = "character".\nas400: Properties  (12122347) : toolbox trace = "".\nas400: Properties  (12122347) : qaqqinilib = "".\nas400: Properties  (12122347) : login timeout = "".\nas400: Properties  (12122347) : true autocommit = "false".\nas400: Properties  (12122347) : bidi implicit reordering = "true".\nas400: Properties  (12122347) : bidi numeric ordering = "false".\nas400: Properties  (12122347) : hold input locators = "true".\nas400: Properties  (12122347) : hold statements = "false".\nas400: Properties  (12122347) : rollback cursor hold = "false".\nas400: Properties  (12122347) : variable field compression = "true".\nas400: Properties  (12122347) : query optimize goal = "0".\nas400: Properties  (12122347) : keep alive = "".\nas400: Properties  (12122347) : receive buffer size = "".\nas400: Properties  (12122347) : send buffer size = "".\nas400: Properties  (12122347) : XA loosely coupled support = "0".\nas400: Properties  (12122347) : translate boolean = "true".\nas400: Properties  (12122347) : metadata source = "-1".\nas400: Properties  (12122347) : query storage limit = "-1".\nas400: Properties  (12122347) : decfloat rounding mode = "half even".\nas400: Properties  (12122347) : autocommit exception = "false".\nas400: Properties  (12122347) : auto commit = "true".\nas400: Properties  (12122347) : ignore warnings = "".\nas400: Properties  (12122347) : secure current user = "true".\nas400: Properties  (12122347) : concurrent access resolution = "0".\nas400: Properties  (12122347) : jvm16 synchronize = "true".\nas400: Properties  (12122347) : socket timeout = "".\nas400: Properties  (12122347) : use block update = "false".\nas400: Properties  (12122347) : maximum blocked input rows = "32000".\nas400: Driver AS/400 Toolbox for Java JDBC Driver (15779934) : Using IBM Toolbox for Java JDBC driver implementation.\nas400: Properties  (12122347) : metadata source = "0".\nas400: Toolbox for Java - Open Source Software, JTOpen 7.3, codebase 5770-SS1 V7R1M0.03 2011/01/14 @B5\nas400: JDBC Level: 40\nas400: Properties  (12122347) : package ccsid = "13488".\nas400: Connection productionServer (367156) : Client CCSID = 13488.\nas400: Connection productionServer (367156) : Setting server NLV = 2912.\nas400: Connection productionServer (367156) : Client functional level = V7R1M01   .\nas400: Connection productionServer (367156) : Data compression = RLE.\nas400: Connection productionServer (367156) : ROWID supported = true.\nas400: Connection productionServer (367156) : True auto-commit supported = true.\nas400: Connection productionServer (367156) : 128 byte column names supported = true.\nas400: Connection productionServer (367156) : Maximum decimal precision = 31.\nas400: Connection productionServer (367156) : Maximum decimal scale = 31.\nas400: Connection productionServer (367156) : Minimum divide scale = 0.\nas400: Connection productionServer (367156) : Translate hex = character.\nas400: Connection productionServer (367156) : query optimize goal = 0.\nas400: Connection productionServer (367156) : Using extended datastreams.\nas400: Connection productionServer (367156) : JDBC driver major version = 9.\nas400: Connection productionServer (367156) : IBM i VRM = V5R4M0.\nas400: Connection productionServer (367156) : Server CCSID = 870.\nas400: Connection productionServer (367156) : Server functional level = V5R4M00014 (14).\nas400: Connection productionServer (367156) : Server job identifier = 692621/QUSER/QZDASOINIT.\nas400: Properties  (12122347) : decimal separator = ".".\nas400: Properties  (12122347) : date format = "dmy".\nas400: Properties  (12122347) : date separator = "/".\nas400: Properties  (12122347) : time format = "hms".\nas400: Properties  (12122347) : time separator = ":".\nas400: Connection productionServer (367156)  open.\nas400: Connection productionServer (367156) : Auto commit = "true".\nas400: Connection productionServer (367156) : Read only = "false".\nas400: Connection productionServer (367156) : Transaction isolation = "1".\nas400: Connection productionServer (367156) : Auto commit = "false".\nas400: PreparedStatement STMT0001 (24864323)  open. Parent: Connection productionServer (367156) .\nas400: PreparedStatement STMT0001 (24864323) : Escape processing = "true".\nas400: PreparedStatement STMT0001 (24864323) : Fetch direction = "1000".\nas400: PreparedStatement STMT0001 (24864323) : Fetch size = "0".\nas400: PreparedStatement STMT0001 (24864323) : Max field size = "0".\nas400: PreparedStatement STMT0001 (24864323) : Max rows = "0".\nas400: PreparedStatement STMT0001 (24864323) : Query timeout = "0".\nas400: PreparedStatement STMT0001 (24864323) : Result set concurrency = "1007".\nas400: PreparedStatement STMT0001 (24864323) : Result set holdability = "1".\nas400: PreparedStatement STMT0001 (24864323) : Result set type = "1003".\nas400: PreparedStatement STMT0001 (24864323) : Behavior Override = "0".\nas400: PreparedStatement STMT0001 (24864323) : Data to correlate statement with cursor Cursor CRSR0001 (7792807) .\nas400: PreparedStatement STMT0001 (24864323) : Preparing [insert into test_v6r1 (id, value ) values (?, ?)].\nas400: PreparedStatement STMT0001 (24864323) : Prepared STMT0001*, SQL Statement -->[insert into test_v6r1 (id, value ) values (?, ?)].\nas400: PreparedStatement STMT0001 (24864323) : setInt().\nas400: PreparedStatement STMT0001 (24864323) : parameter index: 1 value: 1.\nas400: PreparedStatement STMT0001 (24864323) : setString().\nas400: PreparedStatement STMT0001 (24864323) : parameter index: 2 value: Test: 2011-04-27 16:34:30.981.\nas400: PreparedStatement STMT0001 (24864323) : Descriptor 1 created or changed.\nas400: PreparedStatement STMT0001 (24864323) : returnCode is: 0.\nas400: PreparedStatement STMT0001 (24864323) : generated key from system is: null.\nas400: ResultSet  (2850225)  open.\nas400: ResultSet  (2850225) : Conncurrency = "1007".\nas400: ResultSet  (2850225) : Fetch direction = "1000".\nas400: ResultSet  (2850225) : Fetch size = "0".\nas400: ResultSet  (2850225) : Max rows = "0".\nas400: ResultSet  (2850225) : Type = "1004".\nas400: PreparedStatement STMT0001 (24864323) : Executed STMT0001*, SQL Statement --> [insert into test_v6r1 (id, value ) values (?, ?)].\nas400: PreparedStatement STMT0001 (24864323) : Update count = 1.\nas400: PreparedStatement STMT0001 (24864323) : Result set = false.\nas400: PreparedStatement STMT0001 (24864323) : Number of result sets = 0.\nas400: PreparedStatement STMT0001 (24864323) : Row count estimate = -1.\nPS1 - SQL insert affected 1 rows and returned 0 keys\nPS1 - getUpdateCount()=1\nas400: PreparedStatement STMT0002 (19098837)  open. Parent: Connection productionServer (367156) .\nas400: PreparedStatement STMT0002 (19098837) : Escape processing = "true".\nas400: PreparedStatement STMT0002 (19098837) : Fetch direction = "1000".\nas400: PreparedStatement STMT0002 (19098837) : Fetch size = "0".\nas400: PreparedStatement STMT0002 (19098837) : Max field size = "0".\nas400: PreparedStatement STMT0002 (19098837) : Max rows = "0".\nas400: PreparedStatement STMT0002 (19098837) : Query timeout = "0".\nas400: PreparedStatement STMT0002 (19098837) : Result set concurrency = "1007".\nas400: PreparedStatement STMT0002 (19098837) : Result set holdability = "1".\nas400: PreparedStatement STMT0002 (19098837) : Result set type = "1003".\nas400: PreparedStatement STMT0002 (19098837) : Behavior Override = "0".\nas400: PreparedStatement STMT0002 (19098837) : Data to correlate statement with cursor Cursor CRSR0002 (12470752) .\nas400: PreparedStatement STMT0002 (19098837) : Preparing [insert into test_v6r1 (id, value) values (?, ?)].\nas400: PreparedStatement STMT0002 (19098837) : Prepared STMT0002*, SQL Statement -->[insert into test_v6r1 (id, value) values (?, ?)].\nas400: PreparedStatement STMT0002 (19098837) : setInt().\nas400: PreparedStatement STMT0002 (19098837) : parameter index: 1 value: 2.\nas400: PreparedStatement STMT0002 (19098837) : setString().\nas400: PreparedStatement STMT0002 (19098837) : parameter index: 2 value: Test: 2011-04-27 16:34:30.981.\nas400: PreparedStatement STMT0002 (19098837) : Descriptor 2 created or changed.\nas400: PreparedStatement STMT0002 (19098837) : Executed STMT0002*, SQL Statement --> [insert into test_v6r1 (id, value) values (?, ?)].\nas400: PreparedStatement STMT0002 (19098837) : Update count = 1.\nas400: PreparedStatement STMT0002 (19098837) : Result set = false.\nas400: PreparedStatement STMT0002 (19098837) : Number of result sets = 0.\nas400: PreparedStatement STMT0002 (19098837) : Row count estimate = -1.\nPS2 - SQL insert affected 1 rows and returned 0 keys\nPS2 - getUpdateCount()=1\nas400: Connection productionServer (367156) : Testing to see if cursors should be held..\nas400: Connection productionServer (367156) : Transaction commit.\nas400: ResultSet  (2850225)  closed.\nas400: PreparedStatement STMT0001 (24864323)  closed.\nas400: PreparedStatement STMT0002 (19098837)  closed.\nas400: Connection productionServer (367156)  closed.\n
Run Code Online (Sandbox Code Playgroud)\n\n

V6R1控制台输出:

\n\n
Toolbox for Java - Open Source Software, JTOpen 7.3, codebase 5770-SS1 V7R1M0.03 2011/01/14 @B5\n.\n.\n.\nas400: Driver AS/400 Toolbox for Java JDBC Driver (27979955) : Using IBM Toolbox for Java JDBC driver implementation.\nas400: Properties  (16020374) : metadata source = "0".\nas400: Toolbox for Java - Open Source Software, JTOpen 7.3, codebase 5770-SS1 V7R1M0.03 2011/01/14 @B5\nas400: JDBC Level: 40\nas400: Properties  (16020374) : package ccsid = "13488".\nas400: Connection testServer (11463270) : Client CCSID = 13488.\nas400: Connection testServer (11463270) : Setting server NLV = 2912.\nas400: Connection testServer (11463270) : Client functional level = V7R1M01   .\nas400: Connection testServer (11463270) : Data compression = RLE.\nas400: Connection testServer (11463270) : ROWID supported = true.\nas400: Connection testServer (11463270) : True auto-commit supported = true.\nas400: Connection testServer (11463270) : 128 byte column names supported = true.\nas400: Connection testServer (11463270) : Maximum decimal precision = 31.\nas400: Connection testServer (11463270) : Maximum decimal scale = 31.\nas400: Connection testServer (11463270) : Minimum divide scale = 0.\nas400: Connection testServer (11463270) : Translate hex = character.\nas400: Connection testServer (11463270) : query optimize goal = 0.\nas400: Connection testServer (11463270) : query storage limit = -1.\nas400: Connection testServer (11463270) : Using extended datastreams.\nas400: Connection testServer (11463270) : JDBC driver major version = 9.\nas400: Connection testServer (11463270) : IBM i VRM = V6R1M0.\nas400: Connection testServer (11463270) : Server CCSID = 870.\nas400: Connection testServer (11463270) : Server functional level = V6R1M00014 (14).\nas400: Connection testServer (11463270) : Server job identifier = 072485/QUSER/QZDASOINIT.\nas400: Properties  (16020374) : decimal separator = ".".\nas400: Properties  (16020374) : date format = "dmy".\nas400: Properties  (16020374) : date separator = "/".\nas400: Properties  (16020374) : time format = "hms".\nas400: Properties  (16020374) : time separator = ":".\nas400: Connection S65AB7B0 (11463270)  open.\nas400: Connection S65AB7B0 (11463270) : Auto commit = "true".\nas400: Connection S65AB7B0 (11463270) : Read only = "false".\nas400: Connection S65AB7B0 (11463270) : Transaction isolation = "1".\nas400: Connection S65AB7B0 (11463270) : Auto commit = "false".\nas400: PreparedStatement STMT0001 (15696851)  open. Parent: Connection S65AB7B0 (11463270) .\nas400: PreparedStatement STMT0001 (15696851) : Escape processing = "true".\nas400: PreparedStatement STMT0001 (15696851) : Fetch direction = "1000".\nas400: PreparedStatement STMT0001 (15696851) : Fetch size = "0".\nas400: PreparedStatement STMT0001 (15696851) : Max field size = "0".\nas400: PreparedStatement STMT0001 (15696851) : Max rows = "0".\nas400: PreparedStatement STMT0001 (15696851) : Query timeout = "0".\nas400: PreparedStatement STMT0001 (15696851) : Result set concurrency = "1007".\nas400: PreparedStatement STMT0001 (15696851) : Result set holdability = "1".\nas400: PreparedStatement STMT0001 (15696851) : Result set type = "1003".\nas400: PreparedStatement STMT0001 (15696851) : Behavior Override = "0".\nas400: PreparedStatement STMT0001 (15696851) : Data to correlate statement wi