JDBC应用(一)之保存数据

2 篇文章 0 订阅
1 篇文章 0 订阅

1.BILL_SEQUENCE.nextval(序列)

 String sql = "insert into bmis_bill (id,type,patientId,printUser,printTime,createTime,content) 
values(BILL_SEQUENCE.nextval,?,?,?,?,?,?)";
        int result = 0 ;
        try {
             result = jdbcTemplate.update(sql,bill.getType(),bill.getPatientId(),bill.getPrintUser(),bill.getPrintTime(),bill.getCreateTime(),bill.getContent());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;



        Connection conn=null;
		PreparedStatement pstmt=null;
		String sql="insert into mystu(id,name,age,gender) values(myseq.nextval,?,?,?)";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager
                 .getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,"tom");
			pstmt.setInt(2,25);
			pstmt.setString(3,"M");
			pstmt.execute();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭资源
		}

2. 连接数据库操作


//公共代码:得到数据库连接
public Connection getConnection() throws Exception{
	Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
	Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbname", "username", "password");
	return conn;
}

3.方法一,查两次
 

//方法一
//先用select seq_t1.nextval as id from dual 取到新的sequence值。
//然后将最新的值通过变量传递给插入的语句:insert into t1(id) values(?) 
//最后返回开始取到的sequence值。
//这种方法的优点代码简单直观,使用的人也最多,缺点是需要两次sql交互,性能不佳。
public int insertDataReturnKeyByGetNextVal() throws Exception {
	Connection conn = getConnection();
	String vsql = "select seq_t1.nextval as id from dual";
	PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql);
	ResultSet rs=pstmt.executeQuery();
	rs.next();
	int id=rs.getInt(1);
	rs.close();
	pstmt.close();
	vsql="insert into t1(id) values(?)";
	pstmt =(PreparedStatement)conn.prepareStatement(vsql);
	pstmt.setInt(1, id);
	pstmt.executeUpdate();
	System.out.print("id:"+id);
	return id;
}

4.方法二,同一个Connection

//方法二
//先用insert into t1(id) values(seq_t1.nextval)插入数据。
//然后使用select seq_t1.currval as id from dual返回刚才插入的记录生成的sequence值。
//注:seq_t1.currval表示取出当前会话的最后生成的sequence值,由于是用会话隔离,只要保证两个SQL使用同一个Connection即可,对于采用连接池应用需要将两个SQL放在同一个事务内才可保证并发安全。
//另外如果会话没有生成过sequence值,使用seq_t1.currval语法会报错。
//这种方法的优点可以在插入记录后返回sequence,适合于数据插入业务逻辑不好改造的业务代码,缺点是需要两次sql交互,性能不佳,并且容易产生并发安全问题。
public int insertDataReturnKeyByGetCurrVal() throws Exception {
	Connection conn = getConnection();
	String vsql = "insert into t1(id) values(seq_t1.nextval)";
	PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql);
	pstmt.executeUpdate();
	pstmt.close();
	vsql="select seq_t1.currval as id from dual";
	pstmt =(PreparedStatement)conn.prepareStatement(vsql);
	ResultSet rs=pstmt.executeQuery();
	rs.next();
	int id=rs.getInt(1);
	rs.close();
	pstmt.close();
	System.out.print("id:"+id);
	return id;
}

5.方法三,pl/sql语法,不直观

//方法三
//采用pl/sql的returning into语法,可以用CallableStatement对象设置registerOutParameter取得输出变量的值。
//这种方法的优点是只要一次sql交互,性能较好,缺点是需要采用pl/sql语法,代码不直观,使用较少。
public int insertDataReturnKeyByPlsql() throws Exception {
	Connection conn = getConnection();
	String vsql = "begin insert into t1(id) values(seq_t1.nextval) returning id into :1;end;";
	CallableStatement cstmt =(CallableStatement)conn.prepareCall ( vsql); 
	cstmt.registerOutParameter(1, Types.BIGINT);
	cstmt.execute();
	int id=cstmt.getInt(1);
	System.out.print("id:"+id);
	cstmt.close();
	return id;
}

 6.方法4,利用getGeneratedKeys

//方法四
//采用PreparedStatement的getGeneratedKeys方法
//conn.prepareStatement的第二个参数可以设置GeneratedKeys的字段名列表,变量类型是一个字符串数组
//注:对Oracle数据库这里不能像其它数据库那样用prepareStatement(vsql,Statement.RETURN_GENERATED_KEYS)方法,这种语法是用来取自增类型的数据。
//Oracle没有自增类型,全部采用的是sequence实现,如果传Statement.RETURN_GENERATED_KEYS则返回的是新插入记录的ROWID,并不是我们相要的sequence值。
//这种方法的优点是性能良好,只要一次sql交互,实际上内部也是将sql转换成oracle的returning into的语法,缺点是只有Oracle10g才支持,使用较少。
public int insertDataReturnKeyByGeneratedKeys() throws Exception {
	Connection conn = getConnection();
	String vsql = "insert into t1(id) values(seq_t1.nextval)";
	PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql,new String[]{"ID"});
	pstmt.executeUpdate();
	ResultSet rs=pstmt.getGeneratedKeys();
	rs.next();
	int id=rs.getInt(1);
	rs.close();
	pstmt.close();
	System.out.print("id:"+id);
	return id;
}

7.方法5

//方法五
//和方法三类似,采用oracle特有的returning into语法,设置输出参数,但是不同的地方是采用OraclePreparedStatement对象,因为jdbc规范里标准的PreparedStatement对象是不能设置输出类型参数。
//最后用getReturnResultSet取到新插入的sequence值,
//这种方法的优点是性能最好,因为只要一次sql交互,oracle9i也支持,缺点是只能使用Oracle jdbc特有的OraclePreparedStatement对象。
public int insertDataReturnKeyByReturnInto() throws Exception {
	Connection conn = getConnection();
	String vsql = "insert into t1(id) values(seq_t1.nextval) returning id into :1";
	OraclePreparedStatement pstmt =(OraclePreparedStatement)conn.prepareStatement(vsql);
	pstmt.registerReturnParameter(1, Types.BIGINT);
	pstmt.executeUpdate();
	ResultSet rs=pstmt.getReturnResultSet();
	rs.next();
	int id=rs.getInt(1);
	rs.close();
	pstmt.close();
	System.out.print("id:"+id);
	return id;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值