Created
May 23, 2018 12:46
-
-
Save harawata/f1d75e8cb7af919bf63d3a7623f805a2 to your computer and use it in GitHub Desktop.
Testing DB2 behavior regarding https://github.com/mybatis/mybatis-3/issues/1288
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result:
enableExtendedDescribe=1
in the URL didn't change the result.