首页 技术 正文
技术 2022年11月21日
0 收藏 757 点赞 2,795 浏览 4511 个字

获取数据库自动生成的主键

【孤立的技术是没有价值的】,我们这里只是为了了解具体的实现步骤:我们在插入数据的时候,经常会需要获取我们插入的这一行数据对应的主键值。

具体的代码实现:

 /**
* 获取数据库自动生成的主键
*/
@Test
public void testGetKeyValues(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
connection=JDBCTools.getConnection();
String sql="insert into customers(name,email,birth)"+
" values(?,?,?)";
// preparedStatement=connection.prepareStatement(sql);
//我们这里使用重载的prepareStatement(sql,flag)方法
//来生成prepareStatement对象
preparedStatement=connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, "ABCDE");
preparedStatement.setString(2, "abcd@guigu.com");
preparedStatement.setDate(3, new Date(new java.util.Date().getTime()));
preparedStatement.executeUpdate();
//通过getGeneratedKeys方法获取包含了新生成的主键的ResultSet对象
//在ResultSet结果集中,只包含一列,列名:GENERATED_KEY,用于存放新生成的主键值
rs=preparedStatement.getGeneratedKeys();
if(rs.next()){
System.out.println(rs.getObject(1));
}
ResultSetMetaData rsmd=rs.getMetaData();
for(int i=0;i<rsmd.getColumnCount();i++){
System.out.println(rsmd.getColumnName(i+1));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(rs,preparedStatement,connection);
}
}

处理Blob

Blob的基本概念

JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理
JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

1).插入Blob类型数据

    /**
* 插入Blob类型的数据必须使用PreparedStatement
* 因为Blob类型的数据是无法使用字符串拼写的
*
* 调用setBlob(int index,InputStream,inputStream)
*/

具体代码实现:

 @Test
public void testInsertBlod(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
connection=JDBCTools.getConnection();
String sql="insert into customers(name,email,birth,picture)"+
" values(?,?,?,?)";
// preparedStatement=connection.prepareStatement(sql);
//我们这里使用重载的prepareStatement(sql,flag)方法
//来生成prepareStatement对象
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, "ABCDE");
preparedStatement.setString(2, "abcd@guigu.com");
preparedStatement.setDate(3, new Date(new java.util.Date().getTime()));
InputStream inputStream=new FileInputStream("blob.png");
preparedStatement.setObject(4, inputStream);
preparedStatement.executeUpdate(); } catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(rs,preparedStatement,connection);
}
}

2).读取Blob类型数据

/**
* 读取Blob数据:
* 1.使用getBlob方法,读取Blod对象
* 2.调用Blob的getBinaryStream()方法得到输入流,再使用IO操作即可
*/

具体代码实现:

 @Test
public void testReadBlob(){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "select id,name,email,birth,picture"+
" from customers where id =18";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
String email=resultSet.getString(3);
System.out.println(id+":"+name+":"+email);
Blob pictureBlob=resultSet.getBlob(5);
InputStream inputStream=pictureBlob.getBinaryStream();
OutputStream out=new FileOutputStream("flo.png");
byte[] buffer=new byte[1024];
int len =0;
while((len=inputStream.read(buffer))!=-1){
out.write(buffer,0,len);
}
out.close();
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(resultSet, preparedStatement, connection);
}
}

数据库事务

数据库事务概述

JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

数据库事务的四个属性

JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

JDBC的数据库事务

JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

我们做一个小实验:

先建立一个数据表:

JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

试验中要用到的更新数据的通用方法update():

     public static void update(Connection connection,String sql,
Object ...args){
/**
* 执行SQL语句,使用PreparedStatement
*/
PreparedStatement preparedStatement=null;
try {
preparedStatement=connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedStatement.setObject(i+1, args[i]);
}
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(null, preparedStatement, null);
}

我们要完成的是:Tom->Jerry汇款500元
  * 数据库事务
  * 关于事务:
  * 1.如果多个操作,每个操作使用的是自己的单独的连接,则无法保证事务
  * 2.具体步骤:
  * 1).开始事务,取消默认自动提交行为
  * 2).如果事务的操作都成功,则提交事务:connection.commit();
  * 3).回滚事务:若出现异常,则在catch块中回滚事务

我们组织代码就按照上面的步骤来进行.

 public void testTeansaction() throws Exception{
Connection connection=null;
try {
connection=JDBCTools.getConnection();
System.out.println(connection.getAutoCommit());
String sql="update users set balance=balance-500 where id=1";
//开始事务:取消默认提交
connection.setAutoCommit(false);
update(connection,sql);
int i=10/0;
System.out.println(i);
sql="update users set balance=balance+500 where id=2";
JDBCTools.update(sql);
//提交事务
connection.commit();
} catch (Exception e) {
e.printStackTrace();
//回滚事务
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
//关闭连接
JDBCTools.release(null, null, connection);
}
}

可以发现,因为我们使用的是同一个connection连接,当异常(除数为0)发生的时候,事务会发生回滚,数据库的数据会恢复到事务开始之前的状态.


本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/ysw-go/
1、本博客的原创原创文章,都是本人平时学习所做的笔记,如有错误,欢迎指正。
2、如有侵犯您的知识产权和版权问题,请通知本人,本人会即时做出处理文章。
3、本博客的目的是知识交流所用,转载自其它博客或网站,作为自己的参考资料的,感谢这些文章的原创人员

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894