码迷,mamicode.com
首页 > 数据库 > 详细

JDBC(一)之细说JDBC

时间:2017-09-14 21:48:48      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:2.3   atm   dbutils   信息   obj   gettime   first   event   工具类   

Properties info = new Properties();//要参考数据库文档
        info.setProperty("user", "root");
        info.setProperty("password","root");

 

今天这一篇写的是关于JDBC的内容。之前一直在学习mysql数据库,那数据库怎么和我们的程序相互交互呢,它们之间的桥梁就是JDBC。接下来让我们直接进入正题!

一、JDBC概述


1.1、JDBC简介

  JDBC全称为:Java DataBase Connectivity(java数据库连接)

  JDBC是SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范

  JDBC是一组专门负责连接并操作数据库的标准,在整个JDBC 中实际上大量的提供的是接口。由数据库厂商提供,不同数据库其JDBC驱动程序是不同。

  JDBC与数据库驱动之间的关系:接口与实现的关系

1.2、JDBC操作的步骤

  在操作JDBC时,我们大概可以分成四个步骤来完成:

    1)加载数据库驱动程序,加载的时候需要将驱动程序配置到classpath之中

    2)连接数据库,通过Connection 接口和 DriverManager 类完成

    3)操作数据库,通过Statement、PreparedStatement、ResultSet 三个接口完成

    4)关闭数据库,在实际开发中数据库资源非常有限,操作完之后必须关闭

二、JDBC的一个类和三个接口


2.1、java.sql.Drivermanager类 :(注册驱动和创建连接)

  1)注册驱动

    DriverManager.registerDriver(new com.mysql.jdbc.Driver());

     这种我们不推荐使用:一是导致驱动被注册两次,二是强烈依赖数据库的驱动jar包

  2)与数据库建立连接

    static Connection getConnection(String url)

      URL:SUN公司与数据库厂商之间的一种协议

      jdbc:mysql://localhost:3306/Test
      协议 子协议     IP       :端口号 数据库

    DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");

    static Connection getConnection(String url, Properties info) 

      Properties info = new Properties();//要参考数据库文档
	 info.setProperty("user", "root");
      info.setProperty("password","root");

    static Connection getConnection(String url, String user, String password)

    试图建立到给定数据库 URL 的连接

    DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");

2.2、java.sql.Connection接口(一个连接)

  接口的实现在数据库驱动中,所有与数据库交互都是基于连接对象的。

  Statement  createStatement(); //创建操作sql语句的对象

  PreparedStatement  prepareStatement(sql);

2.3、java.sql.Statement接口:(操作sql语句,并返回相应结果的对象(小货车))

  接口的实现在数据库驱动中,用于执行静态 SQL 语句并返回它所生成结果的对象。

  ResultSet  executeQuery(String sql)   根据查询语句返回结果集。只能执行select语句。
     int     executeUpdate(String sql)   根据执行的DML(insert update delete)语句,返回受影响的行数。
  boolean    execute(String sql)    此方法可以执行任意sql语句。返回boolean值,表示是否返回ResultSet结果集。仅当执行select语句,且有返回结果时返回true, 其它语句都返回false。

2.4、java.sql.ResultSet接口:(结果集(客户端存表数据的对象))

  1)封装结果集

    提供一个游标,默认游标指向结果集第一行之前
    调用一次next(),游标向下移动一行
    提供一些get方法

  2)封装数据的方法

  Object getObject(int columnIndex);   根据序号取值,索引从1开始
  Object getObject(String ColomnName);   根据列名取值

  boolean next()   将光标从当前位置向下移动一行
  int getInt(int colIndex)      以int形式获取ResultSet结果集当前行指定列号值
  int getInt(String colLabel)    以int形式获取ResultSet结果集当前行指定列名值
  float getFloat(int colIndex)   以float形式获取ResultSet结果集当前行指定列号值
  float getFloat(String colLabel) 以float形式获取ResultSet结果集当前行指定列名值
  String getString(int colIndex)  以String 形式获取ResultSet结果集当前行指定列号值
  String getString(String colLabel) 以String形式获取ResultSet结果集当前行指定列名值
  Date getDate(int columnIndex); 
  Date getDate(String columnName);
  void close()   关闭ResultSet 对象

    MySQL数据库中的数据类型和Java中的数据类型对应关系:

     技术分享   

  3)可移动游标的方法

  boolean next()  将光标从当前位置向下移一行。 
  boolean previous() 将光标移动到此 ResultSet 对象的上一行。 
  boolean absolute(int row) 参数是当前行的索引,从1开始。根据行的索引定位移动的指定索引行。
  void afterLast() 将光标移动到末尾,正好位于最后一行之后。 
  void beforeFirst() 将光标移动到开头,正好位于第一行之前。

三、细说JDBC连接过程


连接数据库所需要的信息:

  驱动类的全名:com.mysql.jdbc.Driver

  连接数据库的URL:jdbc:mysql://ip:port/db_name?useSSL=true

  用户名:user

  密码:password

3.1、注册驱动

  注册驱动的方式总共有四种:

    第一种:Class.forName("com.mysql.jdbc.Driver");  这种是我们最常用的

    第二种:DriverManager.register(new com,mysql.jdbc.Driver());  

    第三种:System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver);

    第四种:在jvm运行中配置参数 -D jdbc.drivers=com.mysql.jdbc.Driver

3.2、获取(Connection)连接对象

  1)连接MySQL数据库我们需要的数据

    IP、Port、user、password、protocol、schema  

  2)第一种获取Connection对象方式

    String url = protocol+ip+":"+port+"/"+schema+"?user="+user+"&password="+password;
    Connection conn = DriverManager.getConnection(url);

  3)第二种获取Connection的方式    

  Connection conn = DriverManager.getConnection(url,properties);

  3)第三种获取Connection的方式   

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

3.3、获取Statement对象

  Statement stat=conn.createStatement();

3.4、执行SQL语句  

  stat.executeQuery(sql);、stat.execute(sql);、stat.executeUpdate(sql); 

3.5、如果有结果集(ResultSet),则处理结果集

3.6、关闭Statement和Connection的连接,避免计算机资源消耗   

  stat.close();
  conn.close(); 

四、Statement和PrepareStatment


4.1、关系与区别

  关系:PreparedStatement继承自Statement,都是接口。

  区别:PreparedStatement可以使用占位符,是预编译的,批处理比Statement效率高。

4.2、实例显示区别

  1)背景:有一个数据库,里面有个tb_users表,有id,name和passwd三列,然后按照给定的name和password值进行数据查询。

    使用Statement查询:

    sql="select * from tb_users where name=‘"+name+"‘ and passwd=‘"+passwd+"";
    stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);

    使用PrepareStatement查询:

    sql="select * from tb_users where username=? and userpwd=?";
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,name);
    pstmt.setString(2,passwd);
    rs=pstmt.executeQuery();

  2)有一个数据库表tb_books,包含id,name,author,price四列,向该表中插入数据。

    使用Statement:

    sql="insert into tb_books(id,name,author,price) values(‘"+id+"‘,‘"+name+"‘,"+author+",‘"+price+"‘)";
    stmt = conn.createStatement();  
    stmt.executeUpdate(sql); 

    使用PrepareStatement:

    String sql="insert into book(id,name,author,price) values (?,?,?,?)";  
    pstmt = conn.prepareStatement(sql);  
    pstmt.setString(1,var1);
    pstmt.setString(2,var2);
    pstmt.setString(3,var3);
    pstmt.setString(4,var4);
    pstmt.executeUpdate();

4.3、PrepareStatement的优点

  1)PrepareStatement可以提高代码的可读性

  2)ParperStatement提高了代码的灵活性和执行效率

    PrepareStatement接口是Statement接口的子接口,他继承了Statement接口的所有功能。它主要是拿来解决我们使用Statement对象多次执行同一个SQL语句的效率问题的
    ParperStatement接口的机制是在数据库支持预编译的情况下预先将SQL语句编译,当多次执行这条SQL语句时,可以直接执行编译好的SQL语句,这样就大大提高了程序的灵活性和执行效率

  3)使用PrepareStatement比Statement安全 

    举例:用户登录时进行密码验证

    sql="select * from tb_users where name= ‘"+name+"‘ and passwd=‘"+passwd+"";
    stmt = conn.createStatement();  
    rs = stmt.executeUpdate(sql); 

    上面是登录时进行用户名和密码的验证,但是当把‘’or ’1’=’’1’作为密码传递进去会发现如下的SQL语句:

    select * from user where username = user and userpwd=‘‘ or 1=1;

    上面的SQL语句是个永真式。所以不管怎么样都能获取到权限,这还不是最坏的情况。

    当把‘or ‘1‘=1‘;drop table tb_book;当成密码传进去时,会直接删除数据库表,这样的SQL语句就非常的不安全。

    所以使用PrepareStatement可以解决SQL注入攻击的问题

五、JDBC实例


环境:数据库为test_jdbc

   表:s_emp  

5.1、查询数据库中所有的表

技术分享
import org.junit.Test;

import java.sql.*;
import java.util.Properties;

public class BaseOperation{

    @Test
    public void getConn() throws ClassNotFoundException, SQLException{
        // 1.加载驱动:com.mysql.jdbc.Driver,官方文档
        // 在JDBC4.0以后的版本中,可以不显式的指定驱动的类全名
        // 第一种注册驱动:
        Class.forName("com.mysql.jdbc.Driver");
        // 第二种注册驱动:
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        // 第三种注册驱动:
        Properties properties=new Properties();
        // properties.setProperty("driver","com.mysql.jdbc.Driver");
//        Class.forName(properties.getProperty("driver"));
        properties.getProperty("driver");
        // 第四种注册驱动:在运行程序的时候指定JVM参数:
        // -D mysql.driver=com.mysql.jdbc.Driver

        // 2.获取连接对象:Connection
        String protocol="jdbc:mysql://";
        String ip="1.0.0.50";
        int port=5717;
        String schema="briup";
        String user="root";
        String passwd="root";
        String url=protocol+   // 协议
                   ip+":"+     // MySQL服务器的IP
                   port+"/"+   // MySQL的端口
                   schema+     // 数据库名
                   "?user="+user+    // 用户名
                   "&password="+passwd+   // 密码
                   "&useSSL=true";    //在MySQL5.5.*之后的版本中,使用JDBC连接的时候需要该参数。
        System.out.println(url);
        Connection connection=DriverManager.getConnection(url);
        System.out.println(connection);

        // 3.获取Statement对象:
        Statement statement=connection.createStatement();

        // 4.执行SQL语句
        String sql="show tables";
        ResultSet resultSet=statement.executeQuery(sql);
        
        // 5.如果有结果集,处理结果集。
        while(resultSet.next()){
            String tb_names=resultSet.getString(1);
            System.out.println(tb_names);
        }
        // 6.关闭连接
        if(statement!=null) statement.close();
        if(connection!=null) connection.close();
    }
}
BaseOperation

5.2、编写一个JDBC工具类

  1)编写一个db.properties文件

技术分享
driver=com.mysql.jdbc.Driver

protocol=jdbc:mysql://
host_ip=1.0.0.5
port=3306
user=root
password=123456
schema=db_test
db.properties

  2)编写工具类DBUtils类

技术分享
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBUtils{
    private static DBUtils du;
    private static Properties properties;

    // 获取单例对象:
    // 1.私有化构造器
    private DBUtils(){};

    // 2.提供共有的获取对象的方法,该方法中获得的对象时单例的
    public static DBUtils getInstance(){
        //  避免过度加锁行为
        if(du==null){
            synchronized(DBUtils.class){
                // 真正的生成单例对象
                if(du==null){
                    du=new DBUtils();
                }
            }
        }
        return du;
    }

    static{
        properties=new Properties();
        try{
            properties.load(
                ClassLoader.getSystemResourceAsStream(
                    "db.properties"));
            Class.forName(properties.getProperty("driver"));
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public static Connection getConn() throws SQLException{
        String url=properties.getProperty("protocol")
            +properties.getProperty("host_ip")+":"
            +properties.getProperty("port")+"/"
            +properties.getProperty("schema")+"?useSSL=true";
        return DriverManager.getConnection(url,properties);
    }

    public static void close(ResultSet resultSet,
                      Statement statement,
                      Connection connection){
        if(resultSet!=null){
            try{
                resultSet.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }

        if(statement!=null){
            try{
                statement.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try{
                connection.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}
DBUtils

5.3、查询s_emp中的前四行数据

  里面使用两种获得Connection对象的方式

技术分享
import org.junit.Test;

import java.sql.*;
import java.util.Properties;

public class SelectDemo_0010{

    @Test
    public void selectTest_1(){
        ResultSet resultSet=null;
        Connection connection=null;
        Statement statement=null;

        String driver="com.mysql.jdbc.Driver";
        String ip="1.0.0.50";
        String port="5717";
        String schema="test_jdbc";
        String user="root";
        String password="root";

        String url="jdbc:mysql://"
                +ip+":"
                +port+"/"
                +schema
                +"?useSSL=true";
        Properties properties=new Properties();
        properties.setProperty("user",user);
        properties.setProperty("password",password);
        try{
            Class.forName(driver);

            connection=
                DriverManager.getConnection(url,properties);
            System.out.println(connection);

            statement=connection.createStatement();

            String sql="select * from s_emp";
            resultSet=statement.executeQuery(sql);

            while(resultSet.next()){
                String col_1=resultSet.getString(1);
                String col_2=resultSet.getString(2);
                String col_3=resultSet.getString(3);
                String col_4=resultSet.getString(4);
                System.out.println(col_1+"=="+col_2+"=="+col_3+"=="+col_4);
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            if(resultSet!=null){
                try{
                    resultSet.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(statement!=null){
                try{
                    statement.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try{
                    connection.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void selectTest_2(){
        ResultSet resultSet=null;
        Connection connection=null;
        Statement statement=null;

        String driver="com.mysql.jdbc.Driver";
        String ip="1.0.0.50";
        String port="5717";
        String schema="briup";
        String user="root";
        String password="root";

        String url="jdbc:mysql://"
            +ip+":"
            +port+"/"
            +schema
            +"?useSSL=true";
        try{
            Class.forName(driver);

            connection=
                DriverManager.getConnection(url,user,password);
            System.out.println(connection);

            statement=connection.createStatement();

            String sql="select * from s_emp";
            resultSet=statement.executeQuery(sql);

            while(resultSet.next()){
                String col_1=resultSet.getString(1);
                String col_2=resultSet.getString(2);
                String col_3=resultSet.getString(3);
                String col_4=resultSet.getString(4);
                System.out.println(col_1+"==="+col_2+"==="+col_3+"==="+col_4);
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            if(resultSet!=null){
                try{
                    resultSet.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(statement!=null){
                try{
                    statement.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try{
                    connection.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
SelectDemo_0010

5.4、使用工具类获取连接并比较Statement和PrepareStatment的用法

技术分享
import com.briup.bd1702.jdbc.utils.DBUtils;
import org.junit.Test;

import java.sql.*;

public class SelectDemo_0020{

    @Test
    public void selectTest_1(){
        Connection conn=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try{
            conn=DBUtils.getConn();
            statement=conn.createStatement();
            resultSet=statement.executeQuery("show tables");
            while(resultSet.next()){
                System.out.println(resultSet.getString(1));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtils.close(resultSet,statement,conn);
        }
    }

    @Test
    public void selectTest_2(){
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try{
            conn=DBUtils.getConn();
            stmt=conn.createStatement();
            String sql="select last_name as ln,dept_id,start_date,salary from s_emp";
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                String last_name=rs.getString("ln");
                Integer dept_id=rs.getInt("dept_id");
                Date start_date=rs.getDate("start_date");
                Float salary=rs.getFloat("salary");
                System.out.println(last_name+":"
                                   +dept_id+":"
                                   +start_date+":"
                                   +salary);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtils.close(rs,stmt,conn);
        }
    }

    @Test
    public void selectTest_3(){
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;


        String age="53 or 1=1";

        try{
            conn=DBUtils.getConn();
            stmt=conn.createStatement();
            String sql="select name from tb_users where age="+age;
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                System.out.println(rs.getString("name"));
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtils.close(rs,stmt,conn);
        }
    }

    @Test
    public void selectTest_4(){
        Connection conn=null;
        Statement stmt=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;


        String age="53 or 1=1";

        try{
            conn=DBUtils.getConn();
            // 准备SQL语句
            String sql="select name from tb_users where age=?";
            // 获取PreparedStatement对象
            pstmt=conn.prepareStatement(sql);
            // 给SQL语句中的占位符设置值
            pstmt.setInt(1,Integer.parseInt(age));
            rs=pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString("name"));
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtils.close(rs,stmt,conn);
        }
    }
}
SelectDemo_0020

5.5、插入数据,比较Statement和PrepareStatment的插入效率

技术分享

import com.briup.bd1702.jdbc.utils.DBUtils;
import org.junit.Test;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class InsertDemo_0010{

    @Test
    public void insertTest_1(){
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;

        String name="李四";
        Integer age=50;
        Date birth=new Date();
        // 2017-09-14
        Double score=99.5;

        try{
            conn=DBUtils.getConn();
            stmt=conn.createStatement();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String birth2=sdf.format(birth);
            System.out.println(birth2);
            String sql="insert into " +
                "tb_users(name,age,birth,score) " +
                "values(‘"+name+"‘,"+age+",‘"+birth2+"‘,"+score+")";
            stmt.execute(sql);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtils.close(rs,stmt,conn);
        }
    }



    @Test
    public void insertTest_2(){
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;

        try{
            conn=DBUtils.getConn();
            stmt=conn.createStatement();
        }catch(SQLException e){
            e.printStackTrace();
        }

        long oldt=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            String name="李四"+i;
            Integer age=50+i;
            Date birth=new Date(i*1000000);
            Double score=99.5;
            try{
                //转化日期
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                String birth2=sdf.format(birth);
                // SQL语句
                String sql="insert into " +
                    "tb_users(name,age,birth,score) " +
                    "values(‘"+name+"‘,"+age+",‘"+birth2+"‘,"+score+")";
                //执行插入操作
                stmt.execute(sql);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        long newt=System.currentTimeMillis();
        System.out.println("共耗时:"+(newt-oldt));
    }


    @Test
    public void insertTest_3(){
        Connection conn=null;
        Statement stmt=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;

        String name="王五";
        Integer age=50;
        Date birth=new Date();
        Double score=99.5;

        try{
            conn=DBUtils.getConn();
            String sql="insert into " +
                "tb_users(name,age,birth,score) " +
                "values(?,?,?,?)";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setInt(2,age);
            pstmt.setDate(3,new java.sql.Date(birth.getTime()));
            pstmt.setDouble(4,score);
            pstmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtils.close(rs,stmt,conn);
        }
    }



    @Test
    public void insertTest_4(){
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try{
            conn=DBUtils.getConn();
            // SQL语句
            String sql="insert into " +
                "tb_users(name,age,birth,score) " +
                "values(?,?,?,?)";
            pstmt=conn.prepareStatement(sql);
        }catch(SQLException e){
            e.printStackTrace();
        }

        long oldt=System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            String name="李四"+i;
            Integer age=50+i;
            Date birth=new Date(i*1000000);
            Double score=99.5;
            try{
                pstmt.setString(1,name);
                pstmt.setInt(2,age);
                pstmt.setDate(3,new java.sql.Date(birth.getTime()));
                pstmt.setDouble(4,score);
                //执行插入操作
                pstmt.addBatch();
                if(i%3000==0)
                    pstmt.executeBatch();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        try{
            pstmt.executeBatch();
        }catch(SQLException e){
            e.printStackTrace();
        }
        long newt=System.currentTimeMillis();
        System.out.println("共耗时:"+(newt-oldt));
    }
}

InsertDemo_0010

 

觉得不错的点个“推荐”哦!

JDBC(一)之细说JDBC

标签:2.3   atm   dbutils   信息   obj   gettime   first   event   工具类   

原文地址:http://www.cnblogs.com/zhangyinhua/p/7522375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!