通过java,insert数据时返回键值。
结论
- 不仅可以返回主键,还可以返回具体某列的值。
- 只能作用于简单的insert的语句,insert select、merge语句不支持(只测试过ojdbc-14.jar)
- 不支持batch批处理
实例
package database;
//import 略
public class ConnectionTest {
private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String dbUser = "testuser";
private static String dbPwd = "testuser";
private static Connection con;
private static Statement sm = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
private String insertSqlStr = "insert into testtable values(testseq.nextVal, " +
"testseq.nextVal + 100, testseq.nextVal + 1000)";
@BeforeClass
public static void initCon() {
try {
Class.forName(dbDriver);
con = DriverManager.getConnection(dbURL, dbUser, dbPwd);
con.setAutoCommit(false);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@AfterClass
public static void close() {
try {
con.rollback();
con.close();
if (sm != null) {
sm.close();
}
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test(expected= SQLException.class)
public void statementTest1() throws SQLException {
sm = con.createStatement();
sm.execute(insertSqlStr);
rs = sm.getGeneratedKeys();
}
//默认返回rowid
@Test
public void statementTest2() throws SQLException {
sm = con.createStatement();
sm.execute(insertSqlStr, Statement.RETURN_GENERATED_KEYS);
rs = sm.getGeneratedKeys();
printResultSet(rs);
}
@Test
public void statementTest3() throws SQLException {
sm = con.createStatement();
sm.execute(insertSqlStr, new int[]{1, 2, 3});
rs = sm.getGeneratedKeys();
printResultSet(rs);
}
@Test
public void statementTest4() throws SQLException {
sm = con.createStatement();
sm.execute(insertSqlStr, new String[]{"ID", "COL1", "COL2"});
rs = sm.getGeneratedKeys();
printResultSet(rs);
}
@Test(expected= SQLException.class)
public void prepareStatementTest1() throws SQLException {
ps = con.prepareStatement(insertSqlStr);
ps.execute();
rs = ps.getGeneratedKeys();
}
//默认返回rowid
@Test
public void prepareStatementTest2() throws SQLException {
ps = con.prepareStatement(insertSqlStr, Statement.RETURN_GENERATED_KEYS);
ps.execute();
rs = ps.getGeneratedKeys();
printResultSet(rs);
}
@Test
public void prepareStatementTest3() throws SQLException {
ps = con.prepareStatement(insertSqlStr, new int[]{1, 2, 3});
ps.executeUpdate();
rs = ps.getGeneratedKeys();
printResultSet(rs);
}
@Test
public void prepareStatementTest4() throws SQLException {
ps = con.prepareStatement(insertSqlStr, new String[]{"ID", "COL1", "COL2"});
ps.executeUpdate();
rs = ps.getGeneratedKeys();
printResultSet(rs);
}
//批处理不支持返回指定col值
@Test
public void batch1() throws SQLException {
ps = con.prepareStatement("insert into testtable values(testseq.nextVal, ?, ?)",
new String[]{"ID", "COL1", "COL2"});
ps.setInt(1, 101);
ps.setInt(2, 1001);
ps.addBatch();
ps.setInt(1, 102);
ps.setInt(2, 1002);
ps.addBatch();
ps.executeBatch();
// rs = ps.getGeneratedKeys();
// printResultSet(rs);
}
private void printResultSet(ResultSet resultSet) {
StringBuffer priBuffer;
try {
while(resultSet.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
priBuffer = new StringBuffer();
for (int i = 1, len = rsmd.getColumnCount(); i <= len; i++) {
priBuffer.append(rsmd.getColumnName(i)).append(": ")
.append(resultSet.getObject(i)).append(" ");
}
System.out.println(priBuffer.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
spring jdbc 相关API
spring 内部实现是也是基于上面实例