Skip to content

Instantly share code, notes, and snippets.

@harawata
Created May 23, 2018 12:46
Show Gist options
  • Save harawata/f1d75e8cb7af919bf63d3a7623f805a2 to your computer and use it in GitHub Desktop.
Save harawata/f1d75e8cb7af919bf63d3a7623f805a2 to your computer and use it in GitHub Desktop.
Testing DB2 behavior regarding https://github.com/mybatis/mybatis-3/issues/1288
/**
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.sql.*;
import java.util.*;
import java.nio.file.*;
public class Db2GenKeys {
public static void main(String[] args) throws Exception {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url = "jdbc:db2://192.168.62.100:50000/mybatis:enableExtendedDescribe=1;";
String username = "db2inst1";
String password = "db2inst1";
Connection con = DriverManager.getConnection(url, username, password);
{
Statement stmt = con.createStatement();
try {
stmt.execute("drop table gh1288");
} catch (SQLException e) {
//
}
stmt.execute("create table gh1288 (id int primary key generated always as identity, name varchar(16))");
stmt.close();
}
try {
{
System.out.println("INSERT without batch");
PreparedStatement stmt = con.prepareStatement("insert into gh1288 (name) values (?)", Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, "aaa" + 1);
int result = stmt.executeUpdate();
System.out.println(result);
ResultSet rs = stmt.getGeneratedKeys();
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rsmd);
stmt.close();
con.commit();
}
{
System.out.println("INSERT with batch");
PreparedStatement stmt = con.prepareStatement("insert into gh1288 (name) values (?)", Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < 3; i++) {
stmt.setString(1, "aaa" + i);
stmt.addBatch();
}
int[] result = stmt.executeBatch();
System.out.println(Arrays.toString(result));
ResultSet rs = stmt.getGeneratedKeys();
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rsmd);
stmt.close();
con.commit();
}
} finally {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@harawata
Copy link
Author

Result:

INSERT without batch
1
com.ibm.db2.jcc.am.an@1810399e
INSERT with batch
[1, 1, 1]
null
  • DB2 Express-C 11.1
  • db2jcc4-4.24.92.jar
  • enableExtendedDescribe=1 in the URL didn't change the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment