• JDBC数据库的使用操作总结


    JDBC是一组能够执行SQL语句的API

    由于传统的数据库操作方式需要程序员掌握各个不同的数据库的API,极其不便

    因此java定义了JDBC这一标准的接口和类,为程序员操作数据库提供了统一的方式

    JDBC的操作方式比较单一,由五个流程组成:

    1.通过数据库厂商提供的JDBC类库向DriverManager注册数据库驱动

    2.使用DriverManager提供的getConnection()方法连接到数据库

    3.通过数据库的连接对象的createStatement方法建立SQL语句对象

    4.执行SQL语句,并将结果集合返回到ResultSet中

    5.使用while循环读取结果

    6.关闭数据库资源

    下面来看看具体操作Mysql数据库的方法


    准备工作

    首先我们需要建立一个数据库和一张简单的表

     1 mysql> create database person;
     2 Query OK, 1 row affected (0.00 sec)
     3 
     4 mysql> use person;
     5 Database changed
     6 mysql> create table student(
     7     -> id int,
     8     -> name varchar(20),
     9     -> birth year
    10     -> ) default charset=utf8;
    11 Query OK, 0 rows affected (0.10 sec)


    然后往里面插入几条数据

    1 mysql> insert into student values
    2     -> (1,'张三',1990),
    3     -> (2,'李四',1991),
    4     -> (3,'王五',1992);
    5 Query OK, 3 rows affected (0.02 sec)
    6 Records: 3  Duplicates: 0  Warnings: 0


    这样一张简单的表就建好了

    1 mysql> select * from student;
    2 +------+--------+-------+
    3 | id   | name   | birth |
    4 +------+--------+-------+
    5 |    1 | 张三   |  1990 |
    6 |    2 | 李四   |  1991 |
    7 |    3 | 王五   |  1992 |
    8 +------+--------+-------+
    9  rows in set (0.00 sec)

    接下来,去mysql官网下载数据库连接器这个包

    其中这个包里面含有一份文档,里面列举了基本的使用方法,可以参考

    我们的操作也是按照这份文档中的内容进行,然后最主要的地方就是导入这个jar包

    为了操作方便,这里使用eclipse来导入

    右键项目-->构件路径-->添加外部归档,添加好了之后如下所示

    现在我们正式开始使用java来操作mysql数据库(关于这个步骤,上几篇文章有介绍

    JDBC操作实例1:最简单的查询操作

    import java.sql.*;
    
    public class Demo {
        //为了代码紧凑性,暂时抛出所有异常
        public static void main(String[] args) throws Exception {
            //注册数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //建立数据库连接
            //参数一:jdbc:mysql//地址:端口/数据库,参数二:用户名,参数三:密码
            Connection conn = DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/person","root","admin");
            //创建SQL语句
            Statement st = conn.createStatement();
            //执行语句,返回结果
            ResultSet rt = st.executeQuery("show tables");
            //循环取出结果
            while(rt.next()) {
                //获取字段
                System.out.println(rt.getString("Tables_in_person"));
            }
            //关闭资源,最先打开的最后关
            rt.close();
            st.close();
            conn.close();
        }
    }

    运行结果:student

    如此便可执行show tables语句查询出当前数据库含有多少张表

    其中rt.getString()方法是获取字段,这点需要注意

    关闭资源的方式也与以往相反

    不过,上面的操作方式灵活性不大,并且不严谨

    实例2:优化的查询操作

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.ResultSet;
     4 import java.sql.SQLException;
     5 import java.sql.Statement;
     6 
     7 public class Demo {
     8     public static void main(String[] args) {
     9         String url = "jdbc:mysql://localhost:3306/person";
    10         String user = "root";
    11         String pwd = "admin";
    12         String sql = "select * from student";
    13 
    14         Connection conn = null;
    15         Statement st = null;
    16         ResultSet rs = null;
    17         try {
    18             Class.forName("com.mysql.jdbc.Driver");
    19             conn = DriverManager.getConnection(url,user,pwd);
    20             st = conn.createStatement();
    21             //执行查询语句,另外也可以用execute(),代表执行任何SQL语句
    22             rs = st.executeQuery(sql);
    23             while(rs.next()) {
    24                 System.out.println(rs.getObject(1) + "  " +
    25                         rs.getObject(2) + "  " + rs.getInt("birth"));
    26             }
    27         //分别捕获异常
    28         } catch (ClassNotFoundException e) {
    29             e.printStackTrace();
    30         } catch (SQLException e) {
    31             e.printStackTrace();
    32         } finally {
    33             try {
    34                 //判断资源是否存在
    35                 if(rs != null) {
    36                     rs.close();
    37                     //显示的设置为空,提示gc回收
    38                     rs = null;
    39                 }
    40                 if(st != null) {
    41                     st.close();
    42                     st = null;
    43                 }
    44                 if(conn != null) {
    45                     conn.close();
    46                     conn = null;
    47                 }
    48             } catch (SQLException e) {
    49                 e.printStackTrace();
    50             }   
    51         }
    52     }
    53 }

    这里把异常给分别捕获了,并且相关的字符串全部用变量定义

    需要注意下循环取出数据里面的getInt()方法,此处必须知道类型和字段才能取出

    如果不知道可以使用getObject(1)取出第一列,getObject(2)取出第二列,以此类推

    实例3:自定义变量插入到数据库

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Demo {
        public static void main(String[] args) {
            //参数检查
            if (args.length != 3) {
                System.out.println("参数形式不对");
                System.exit(0);
            }
            String id = args[0];
            String name = args[1];
            String birth = args[2];
            String sql = "insert into student values(" + id + ",'" + name +
                    "'," + "'" + birth + "')";
            System.out.println(sql);
    
            String url = "jdbc:mysql://localhost:3306/person";
            String user = "root";
            String pwd = "admin";
    
            Connection conn = null;
            Statement st = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url,user,pwd);
                st = conn.createStatement();
                //注意,此处是excuteUpdate()方法执行
                st.executeUpdate(sql);       
            //分别捕获异常
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(st != null) {
                        st.close();
                        st = null;
                    }
                    if(conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }   
            }
        }
    }

    这里运行需要设置自变量,窗口中右键-->运行方式-->运行配置

    然后在自变量里面写4 susan 1993,我没有写中文,因为产生乱码,目前还不清楚原因

    需要注意的是,执行插入的SQL语句比较难写,最好是打印出SQL语句用以检查

    实例4:PreparedStatement应用

    从上面的Demo可以看到,插入数据的时候,SQL操作相当不便

    这里可以使用PreparedStatement对象来简化SQL语句的建立

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.PreparedStatement;
     4 import java.sql.SQLException;
     5 
     6 public class Demo {
     7 
     8     public static void main(String[] args) {
     9         if (args.length != 3) {
    10             System.out.println("参数形式不对");
    11             System.exit(0);
    12         }
    13         String id = args[0];
    14         String name = args[1];
    15         String birth = args[2];
    16 
    17         String url = "jdbc:mysql://localhost:3306/person";
    18         String user = "root";
    19         String pwd = "admin";
    20 
    21         Connection conn = null;
    22         //声明PreparedStatement对象的引用
    23         PreparedStatement pst = null;
    24         try {
    25             Class.forName("com.mysql.jdbc.Driver");
    26             conn = DriverManager.getConnection(url,user,pwd);
    27             //使用?代替变量
    28             pst = conn.prepareStatement("insert into student values (?,?,?)");
    29             //给指定参数的位置设定变量
    30             pst.setString(1, id);
    31             pst.setString(2, name);
    32             pst.setString(3, birth);
    33             pst.executeUpdate();
    34         } catch (ClassNotFoundException e) {
    35             e.printStackTrace();
    36         } catch (SQLException e) {
    37             e.printStackTrace();
    38         } finally {
    39             try {
    40                 if(pst != null) {
    41                     pst.close();
    42                     pst = null;
    43                 }
    44                 if(conn != null) {
    45                     conn.close();
    46                     conn = null;
    47                 }
    48             } catch (SQLException e) {
    49                 e.printStackTrace();
    50             }   
    51         }
    52     }
    53 }

    实例5:Batch批处理

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.SQLException;
     4 import java.sql.Statement;
     5 
     6 public class Demo {
     7 
     8     public static void main(String[] args) {
     9 
    10         String url = "jdbc:mysql://localhost:3306/person";
    11         String user = "root";
    12         String pwd = "admin";
    13 
    14         Connection conn = null;
    15         Statement st = null;
    16         try {
    17             Class.forName("com.mysql.jdbc.Driver");
    18             conn = DriverManager.getConnection(url,user,pwd);
    19             st = conn.createStatement();
    20             //添加批处理
    21             st.addBatch("insert into student values(6,'Jerry','1995')");
    22             st.addBatch("insert into student values(7,'Greg','1996')");
    23             st.addBatch("insert into student values(8,'Ryan','1997')");
    24             //执行批处理
    25             st.executeBatch();
    26         } catch (ClassNotFoundException e) {
    27             e.printStackTrace();
    28         } catch (SQLException e) {
    29             e.printStackTrace();
    30         } finally {
    31             try {
    32                 if(st != null) {
    33                     st.close();
    34                     st = null;
    35                 }
    36                 if(conn != null) {
    37                     conn.close();
    38                     conn = null;
    39                 }
    40             } catch (SQLException e) {
    41                 e.printStackTrace();
    42             }   
    43         }
    44     }
    45 }

    批处理比较简单,只需先建立Statement对象,然后逐个添加批处理即可

    最后使用executeBatch()方法执行批处理

    此外,PreparedStatement对象也可以使用批处理

    1 PreparedStatement ps = conn.prepareStatement("insert into student values(?,?,?)");
    2 ps.setInt(1,8);
    3 ps.setString(2,"GG");
    4 ps.setString(3,"1996");
    5 ps.addBatch();
    6 ps.executeBatch();

    实例6:Transaction事务处理

    事务处理是要求sql以单元的形式更新数据库,要求其确保一致性

    如银行的转账业务,一方转出后,另一方则增加

    如果出现异常,那么所有的操作则会回滚

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.SQLException;
     4 import java.sql.Statement;
     5 
     6 public class Demo {
     7 
     8     public static void main(String[] args) {
     9 
    10         String url = "jdbc:mysql://localhost:3306/person";
    11         String user = "root";
    12         String pwd = "admin";
    13 
    14         Connection conn = null;
    15         Statement st = null;
    16         try {
    17             Class.forName("com.mysql.jdbc.Driver");
    18             conn = DriverManager.getConnection(url,user,pwd);
    19             //取消自动提交
    20             conn.setAutoCommit(false);
    21             st = conn.createStatement();
    22             st.addBatch("insert into student values(6,'Jerry','1995')");
    23             st.addBatch("insert into student values(7,'Greg','1996')");
    24             st.addBatch("insert into student values(8,'Ryan','1997')");
    25             st.executeBatch();
    26             //提交后设置自动提交
    27             conn.commit();
    28             conn.setAutoCommit(true);
    29         } catch (ClassNotFoundException e) {
    30             e.printStackTrace();
    31 
    32         } catch (SQLException e) {
    33             e.printStackTrace();
    34 
    35             if(conn != null) {
    36                 try {
    37                     //出现异常则回滚操作,然后设置自动提交
    38                     conn.rollback();
    39                     conn.setAutoCommit(true);
    40                 } catch (SQLException e1) {
    41                     e1.printStackTrace();
    42                 }
    43             }
    44         } finally {
    45             try {
    46                 if(st != null) {
    47                     st.close();
    48                     st = null;
    49                 }
    50                 if(conn != null) {
    51                     conn.close();
    52                     conn = null;
    53                 }
    54             } catch (SQLException e) {
    55                 e.printStackTrace();
    56             }   
    57         }
    58     }
    59 }
  • 相关阅读:
    适配器模式(Adapter)
    状态模式(State)
    观察者模式(Publish/Subscribe)
    建造者模式(Builder)
    数据库,知识点汇总
    数据库
    css样式大全
    遮罩层
    js数组冒泡
    js基本方法
  • 原文地址:https://www.cnblogs.com/zhangyongjian/p/3647476.html
Copyright © 2020-2023  润新知