※ 本文轉寄自 tomin.bbs@tomin.twbbs.org
標題 三種獲得自動生成鍵的方法,getGeneratedKeys,專用SQL和可更新的結果集
時間 2010/06/10 Thu 17:21:47
http://blog.csdn.net/java2000_net/archive/2008/09/27/2989625.aspx
/**
* 使用JDBC 3.0提供的 getGeneratedKeys。推薦使用
*
* @param con
* @return
* @throws Exception
*/
public static long getPK1(Connection con) throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO t_type (name)
values ('Can I Get the Auto Increment Field?')",
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromApi = -1;
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
autoIncKeyFromApi = rs.getInt(1);
}
return autoIncKeyFromApi;
}
/**
* 使用數據庫自己的特殊SQL.
*
* @param con
* @return
* @throws Exception
*/
public static long getPK2(Connection con) throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO t_type (name)
values ('Can I Get the Auto Increment Field?')",
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromFunc = -1;
ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
autoIncKeyFromFunc = rs.getInt(1);
}
return autoIncKeyFromFunc;
}
/**
* 使用可更新的結果集。
*
* @param con
* @return
* @throws Exception
*/
public static long getPK3(Connection con) throws Exception {
Statement stmt = con.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM t_Type");
rs.moveToInsertRow();
rs.updateString("name", "AUTO INCREMENT here?");
rs.insertRow();
rs.last();
int autoIncKeyFromRS = rs.getInt("id");
return autoIncKeyFromRS;
}
--
--
看板 coding
作者 標題 三種獲得自動生成鍵的方法,getGeneratedKeys,專用SQL和可更新的結果集
時間 2010/06/10 Thu 17:21:47
http://blog.csdn.net/java2000_net/archive/2008/09/27/2989625.aspx
/**
* 使用JDBC 3.0提供的 getGeneratedKeys。推薦使用
*
* @param con
* @return
* @throws Exception
*/
public static long getPK1(Connection con) throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO t_type (name)
values ('Can I Get the Auto Increment Field?')",
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromApi = -1;
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
autoIncKeyFromApi = rs.getInt(1);
}
return autoIncKeyFromApi;
}
/**
* 使用數據庫自己的特殊SQL.
*
* @param con
* @return
* @throws Exception
*/
public static long getPK2(Connection con) throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO t_type (name)
values ('Can I Get the Auto Increment Field?')",
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromFunc = -1;
ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
autoIncKeyFromFunc = rs.getInt(1);
}
return autoIncKeyFromFunc;
}
/**
* 使用可更新的結果集。
*
* @param con
* @return
* @throws Exception
*/
public static long getPK3(Connection con) throws Exception {
Statement stmt = con.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM t_Type");
rs.moveToInsertRow();
rs.updateString("name", "AUTO INCREMENT here?");
rs.insertRow();
rs.last();
int autoIncKeyFromRS = rs.getInt("id");
return autoIncKeyFromRS;
}
--
◤◥ Origin: Loess Plateau˙黃土高原 tomin.twbbs.org
◣◢ Author: tomin 從 tomin.mdorm.ntnu.edu.tw 發表
--
※ 看板: tomin 文章推薦值: 0 目前人氣: 0 累積人氣: 73
回列表(←)
分享