mysql 我有一个表aa (id ,a,b superid)
其中噢id是自增长字段, superid整型我在java里执行下面的sql
insert into aa(a,b, superid) values (.....) ------这里要求superid等于他的自增长id的值 , 同行现在这个sql执行完后才能看到自增长后的id值,
现在我想在表里设置 superid等于他的自增长id的值 (superid和id在同行)。这个如何在表里设置? (如何可以做的话)
其中噢id是自增长字段, superid整型我在java里执行下面的sql
insert into aa(a,b, superid) values (.....) ------这里要求superid等于他的自增长id的值 , 同行现在这个sql执行完后才能看到自增长后的id值,
现在我想在表里设置 superid等于他的自增长id的值 (superid和id在同行)。这个如何在表里设置? (如何可以做的话)
Query OK, 0 rows affected (0.03 sec)mysql> insert into t2(col2, col3) values('hex', null); update t2 set col3 = last_insert_id() where id = last_insert_id();
Query OK, 1 row affected (0.02 sec)Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from t2;
+----+------+------+
| id | col2 | col3 |
+----+------+------+
| 1 | hex | 1 |
+----+------+------+
1 row in set (0.00 sec)mysql> insert into t2(col2, col3) values('hex', null); update t2 set col3 = last_insert_id() where id = last_insert_id();
Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from t2;
+----+------+------+
| id | col2 | col3 |
+----+------+------+
| 1 | hex | 1 |
| 2 | hex | 2 |
+----+------+------+
2 rows in set (0.00 sec)
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.ResultSet;
6. import java.sql.Statement;
7.
8. /**
9. * 三种获得自动生成主键的方法。
10. *
11. * @author 赵学庆 www.java2000.net
12. *
13. */
14. public class TestGetPK {
15.
16. public static void main(String[] args) throws Exception {
17. Class.forName("com.gbase.jdbc.Driver");
18. String url = "jdbc:gbase://localhost/mytest";
19. Connection con = DriverManager.getConnection(url, "root", "111111");
20.
21. System.out.println(getPK1(con));
22. System.out.println(getPK2(con));
23. System.out.println(getPK3(con));
24. }
25.
26. /**
27. * 使用JDBC 3.0提供的 getGeneratedKeys。推荐使用
28. *
29. * @param con
30. * @return
31. * @throws Exception
32. */
33. public static long getPK1(Connection con) throws Exception {
34. Statement stmt = con.createStatement();
35. stmt.executeUpdate("INSERT INTO t_type (name) values ('Can I Get the Auto Increment Field?')",
36. Statement.RETURN_GENERATED_KEYS);
37.
38. int autoIncKeyFromApi = -1;
39. ResultSet rs = stmt.getGeneratedKeys();
40. if (rs.next()) {
41. autoIncKeyFromApi = rs.getInt(1);
42. }
43. return autoIncKeyFromApi;
44. }
45.
46. /**
47. * 使用数据库自己的特殊SQL.
48. *
49. * @param con
50. * @return
51. * @throws Exception
52. */
53. public static long getPK2(Connection con) throws Exception {
54. Statement stmt = con.createStatement();
55. stmt.executeUpdate("INSERT INTO t_type (name) values ('Can I Get the Auto Increment Field?')",
56. Statement.RETURN_GENERATED_KEYS);
57.
58. int autoIncKeyFromFunc = -1;
59. ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
60. if (rs.next()) {
61. autoIncKeyFromFunc = rs.getInt(1);
62. }
63. return autoIncKeyFromFunc;
64. }
65.
66. /**
67. * 使用可更新的结果集。
68. *
69. * @param con
70. * @return
71. * @throws Exception
72. */
73. public static long getPK3(Connection con) throws Exception {
74. Statement stmt = con.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
75. java.sql.ResultSet.CONCUR_UPDATABLE);
76. ResultSet rs = stmt.executeQuery("SELECT * FROM t_Type");
77. rs.mov
__________________________________
很需要朋友ChinaUnix虚拟化版块全新推出!| Oracle顶级认证,OCM:高薪的象征 | 《开源时代》2010年4月期 | 众说纷“云” 云计算博客征文活动!
liyihongcug * 发短消息
* 加为好友liyihongcug 当前在线UID
91551
帖子
1182
精华
0
积分
4351
可用积分
4351
信誉积分
100
专家积分
0 (本版:)
空间积分
0
阅读权限
50
性别
男
在线时间
431 小时
注册时间
2004-07-02
最后登录
2010-05-21 个人空间 查看详细资料天使帖子
1182
主题
482
精华
0
可用积分
4351
专家积分
0 (本版:0)
在线时间
431 小时
注册时间
2004-07-02
最后登录
2010-05-21 状态:...当前在线...[微博] [博客] [短信]
顶部 [评分] [回复] [引用] [编辑]
3楼 发表于 2010-04-01 18:55 | 只看该作者int nLastID=isLastInsertID(conn);
if(user.getEmail()!= null)
{int nSuperId=isEmailExists(conn,user.getEmail());if(nSuperId >0 )
{
updateSuperID(conn,nSuperId,nLastID);
DAO.insertLimitBySuperid(conn, nLastID+"", nSuperId+"");
}
else
{
updateSuperID(conn,nLastID,nLastID);
DAO.insertLimitBySuperid(conn, nLastID+"", nLastID+"");
}
}
;
user.setId(nLastID);} finally {
DBConnection.closeRS(rs);
DBConnection.closeStmt(ps);
}
public static int isUserExists (Connection conn, String userName) throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(_sqlisUserExists);ps.setString(1, userName);
ps.setLong(2, passwrod);rs = ps.executeQuery();if (rs.next()) {
return rs.getInt(1);
}
} finally {
DBConnection.closeRS(rs);
DBConnection.closeStmt(ps)
}return -1;
}这个问题完结
update ----------------ps.executeUpdate ();
__________________________________
很需要朋友ChinaUnix虚拟化版块全新推出!| Oracle顶级认证,OCM:高薪的象征 | 《开源时代》2010年4月期 | 众说纷“云” 云计算博客征文活动!
liyihongcug * 发短消息
* 加为好友liyihongcug 当前在线UID
91551
帖子
1182
精华
0
积分
4351
可用积分
4351
信誉积分
100
专家积分
0 (本版:)
空间积分
0
阅读权限
50
性别
男
在线时间
431 小时
注册时间
2004-07-02
最后登录
2010-05-21 个人空间 查看详细资料天使帖子
1182
主题
482
精华
0
可用积分
4351
专家积分
0 (本版:0)
在线时间
431 小时
注册时间
2004-07-02
最后登录
2010-05-21 状态:...当前在线...[微博] [博客] [短信]
顶部 [评分] [回复] [引用] [编辑]
4楼 发表于 2010-04-01 20:18 | 只看该作者上面代码没哟 考虑事务 应该说是一个原子操作
Connection conn = null;
try {
conn = DBConnectionManager.getConnection(DBConnectionManager.yourSQLconnecton);
conn.setAutoCommit(false);
//partnerVO.setPassword(password)
PartnerDAO.savePartnerUser(conn, partnerDemoUserVO, of);
UserDAO.insertUser(conn, createdUser, createdUser.get_password(), of);---------调用上面的代码 //liviuM: we will create a new User record, with a new ID, so, it's useless to select first roles from dababase
for(UserRoleVO urVO: createdUser.getRoleList()) {
UserDAO.addRole(conn, createdUser.getId(), urVO);
}
LoggingServletRequestListener.createAccount(conn, createdUser);//
conn.commit();
request.setAttribute("message","user "+createdUser.getUserName()+" created");
} catch (SQLException e) {
conn.rollback();
request.setAttribute("message","user register failed:"+e.getMessage());
} finally {
conn.setAutoCommit(true);
DBConnectionManager.freeConnection(conn);
}
可以看到用的是 一个事务