java jdbc 学习

通过孤傲苍狼的博客学习了一下jdbc,主要总结一下知识点。

原文地址

http://www.cnblogs.com/xdp-acl/p/3946207.html


1、 jdbc时java与数据库的连接api(与数据库的连接应该还有更底层的驱动程序)需要配置数据库连接的地址,驱动,用户名、密码等等信息。

2、 java数据库的操作,主要用一下类Connection,Statement,ResultSet

connection:

A connection(session) with a specific database. SQL statements are executed and results arereturned within the context of a connection.

A Connection object's database is able to provideinformation describing its tables, its supported SQL grammar, its storedprocedures, the capabilities of this connection, and so on. This information isobtained with the getMetaData method.

Statement

The object used forexecuting a static SQL statement and returning the results it produces.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if thereading of one ResultSet object isinterleaved with the reading of another, each must have been generated bydifferent Statement objects. Allexecution methods in the Statement interface implicitlyclose a statment's current ResultSet object if an openone exists.

ResultSet

A table of datarepresenting a database result set, which is usually generated by executing astatement that queries the database.

创建Connection对象(需要配置连接信息),然后通过connection对象创建sql语句执行的对象statement,statement对象执行sql语句之后,返回结果集resulset。

3、Statement又分为PreparedStatement和Statement

PreparedStatement做预处理,对于批量的相同sql语句有优势,并且PreparedStatement能够防止sql注入攻击(sql语句的拼接)。

4、 数据库事务。事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功

 

conn.setAutoCommit(false);//通知数据库开启事务(start transaction)

            String sql1 = "updateaccount set money=money-100 where name='A'";

            st = conn.prepareStatement(sql1);

            st.executeUpdate();

            String sql2 = "updateaccount set money=money+100 where name='B'";

            st = conn.prepareStatement(sql2);

            st.executeUpdate();

            conn.commit();//上面的两条SQL执行Update语句成功之后就通知数据库提交事务(commit)

 

事务的四大特性(ACID)

4.1、原子性(Atomicity)

  
原子性是指事务是一个不可分割的工作单位,事务中的操作要么全部成功,要么全部失败。比如在同一个事务中的SQL语句,要么全部执行成功,要么全部执行失败

4.2、一致性(Consistency)

  
官网上事务一致性的概念是:事务必须使数据库从一个一致性状态变换到另外一个一致性状态。以转账为例子,A向B转账,假设转账之前这两个用户的钱加起来总共是2000,那么A向B转账之后,不管这两个账户怎么转,A用户的钱和B用户的钱加起来的总额还是2000,这个就是事务的一致性。

4.3、隔离性(Isolation)

  
事务的隔离性是多个用户并发访问数据库时,数据库为每一个用户开启的事务,不能被其他事务的操作数据所干扰,多个并发事务之间要相互隔离。

4.4、持久性(Durability)


  持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来即使数据库发生故障也不应该对其有任何影响

 

       我觉得数据库的隔离性大概就多线程并发访问数据库而引起的问题。

例如:A——B转帐,对应于如下两条sql语句
    update from account set money=money+100 wherename='B';
    update from account set money=money-100 wherename='A';

 

5、 从软件设计的层面来讲,下面的代码应该是不太理想的。

String url = "jdbc:mysql://localhost:3306/jdbcStudy";

        //连接的数据库时使用的用户名

        String username = "root";

        //连接的数据库时使用的密码

        String password = "xxxx";

       

        //1.加载驱动,

        //不推荐使用这种方式来加载驱动

//        DriverManager.registerDriver(newcom.mysql.jdbc.Driver());

        Class.forName("com.mysql.jdbc.Driver");//推荐使用这种方式来加载驱动

        //2.获取与数据库的链接

        Connection conn = DriverManager.getConnection(url, username, password);

       

        //3.获取用于向数据库发送sql语句的statement

        Statement st = conn.createStatement();

       

        String sql = "selectid,name,password,email,birthday from users";

        //4.向数据库发sql,并获取代表结果集的resultset

        ResultSet rs = st.executeQuery(sql);

       

        //5.取出结果集的数据

        while(rs.next()){

            System.out.println("id=" + rs.getObject("id"));

            System.out.println("name="+ rs.getObject("name"));

            System.out.println("password="+ rs.getObject("password"));

            System.out.println("email="+ rs.getObject("email"));

            System.out.println("birthday="+ rs.getObject("birthday"));

        }

       

        //6.关闭链接,释放资源

        rs.close();

        st.close();

       conn.close();

A、 数据库连接的参数读取,需要解耦

B、 获取连接需要抽离,由于数据库连接的创建很花时间,最好弄成数据库连接池

C、 对象实体与数据库记录的转换,model转sql利用prepareStatement,记录转对象利用metadata,反射等


开源的框架commons-dbutils。

原文地址

http://www.cnblogs.com/xdp-acl/p/3946207.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值