欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

oracle jdbc 获取自动生成的键值,int id primary key auto_increment;

程序员文章站 2022-03-16 15:37:06
...

用jdbc getGenerationKey属性
================================

public static void main(String args[]) {
       
        	String sql="insert into cdml_sensky_user(uuid,version) values('123','123456789')";
        	int rows = -1;
            try {
            	Class.forName("oracle.jdbc.driver.OracleDriver");
            	   Connection conn = DriverManager
            	    .getConnection("jdbc:oracle:thin:@ip:1521:dbname","username","pwd");    //连接数据库
            	conn.setAutoCommit(false);       //不自动提交
//            	Connection conn =pool.getConnection();
            	PreparedStatement pstat =conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            	//创建一个默认 PreparedStatement 对象,该对象能获取自动生成的键,适合insert语句

            	//(该语句能自动生成键值)autoGeneratedKeys - 指示是否应该返回自动生成的键的标志

            	//它是 Statement.RETURN_GENERATED_KEYS 或 Statement.NO_GENERATED_KEYS 之一

            	Statement stat = conn.createStatement();

//            	pstat.setInt(1, -1);
//            	pstat.setString(2,title);
//            	pstat.setString(3,content);
            	pstat.executeUpdate();

            	ResultSet rsKey = pstat.getGeneratedKeys();      //ResultSet 指示键值
            	rsKey.next();
            	int key = rsKey.getInt(1);     //得到第一个键值
            	rsKey.close();
            	stat.executeUpdate("update cdml_sensky_user set uuid = " + key + " where id = " + key);

            	conn.commit();
            	conn.setAutoCommit(true);     //设回自动提交
            	pstat.close();
            	conn.close();
                	System.out.println("111");
             



                //System.out.println("num:"+num);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //return rows;

    }

 


http://hi.baidu.com/%BD%D6%D0?%F3%B5%C1/blog/item/0d3bf6fd3e4226fbfc037fda.html
获取自动生成的键值,int id primary key auto_increment;
=======================================================================================
java.sql.SQLException: 不支持的特性
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.dbaccess.DBError.throwUnsupportedFeatureSqlException(DBError.java:690)
at oracle.jdbc.driver.OracleConnection.prepareStatement(OracleConnection.java:3452)
at db.NewDBManager.main(NewDBManager.java:131)
=====================================================================================

据张P说Oracle11g可以支持新特性,现在用的方法是存储过程

create or replace procedure USER_REGIST(p_version in varchar2,p_userid  out number) is 
  /** 
  * java书城用mtk用户表注册 
  */ 
begin 
  p_userid := -1; 
  insert into cdml_sensky_user 
    (id,uuid,VERSION) 
  values 
    (p_userid,p_userid,p_version) 
  returning id into p_userid;   --dbms_output.put_line(p_userid); 
   update cdml_sensky_user set uuid=to_char(p_userid) where id=p_userid; 
  commit; 
exception 
  when others then 
    rollback; 
    p_userid := -1; 
end USER_REGIST; 
17:53 2011-6-24 
declare user_id int; 
begin 

USER_REGIST(123456789,user_id); 
dbms_output.put_line(user_id); 
end;