zoukankan      html  css  js  c++  java
  • jdbc java数据库连接 3)Statement接口之执行DDL和DML语句的简化

    上一章的代码中,可以发现,jdbc执行DDL和DML有几个步骤都是一样的:

       1)执行语句开始时,创建驱动注册对象、获取连接的数据库对象、创建Statement对象

    1             // 创建驱动注册对象
    2             Class.forName("com.mysql.jdbc.Driver");
    3 
    4             // 获取连接的数据库对象
    5              Connection conn = DriverManager.getConnection(url, user,
    6                     password);
    7 
    8             // 创建Statement对象
    9              Statement stsm = conn.createStatement();

      

        2)所有操作执行完后,关闭连接(后来的先关)

     1             if (stsm != null) {
     2                 try {
     3                     stsm.close();
     4                 } catch (SQLException e) {
     5                     // TODO Auto-generated catch block
     6                     e.printStackTrace();
     7                     throw new RuntimeException(e);
     8                 }
     9             }
    10             if (conn != null) {
    11                 try {
    12                     conn.close();
    13                 } catch (SQLException e) {
    14                     // TODO Auto-generated catch block
    15                     e.printStackTrace();
    16                     throw new RuntimeException(e);
    17                 }

    所以,我们可以把这三大部分凑起来,建立一个jdbc的工具类:

      jdbcutil

        

     1 /**
     2  * 这是jdbc执行DDL和DML的工具类
     3  * 
     4  * @author LZl
     5  * 
     6  */
     7 public class Jdbcutil {
     8 
     9     // 创建数据库的基本信息
    10     // 创建url
    11     private static String url = "jdbc:mysql://localhost:3306/day1029?useUnicode=true&characterEncoding = GB2312 ";
    12     // 数据库的用户名和密码
    13     private static String user = "root";
    14     private static String password = "root";
    15     public static Connection conn = null;
    16     static Statement stsm = null;
    17 
    18     /**
    19      * 一:注册的驱动程序 获取连接对象的方法 静态代码块(好处是只需要加载一次,且随着类的加载而加载)
    20      */
    21 
    22     static {
    23         try {
    24             Class.forName("com.mysql.jdbc.Driver");
    25         } catch (Exception e) {
    26             e.printStackTrace();
    27             System.out.println("获取数据库连接对象出错");
    28         }
    29     }
    30 
    31     /**
    32      * 二:获取连接对象 该方法返回一个连接
    33      */
    34 
    35     public static Connection getConnection() {
    36 
    37         // 创建连接对象
    38         try {
    39             conn = DriverManager.getConnection(url, user, password);
    40         } catch (SQLException e) {
    41             // TODO Auto-generated catch block
    42             e.printStackTrace();
    43             throw new RuntimeException(e);
    44         }
    45         return conn;
    46 
    47     }
    48 
    49     /**
    50      * 三:释放资源,断开连接 参数列表:conn。stsm
    51      */
    52 
    53     public static void close(Connection conn, Statement stsm) {
    54 
    55         if (stsm != null) {
    56             try {
    57                 stsm.close();
    58             } catch (SQLException e) {
    59                 // TODO Auto-generated catch block
    60                 e.printStackTrace();
    61                 throw new RuntimeException(e);
    62             }
    63         }
    64 
    65         if (conn != null) {
    66             try {
    67                 conn.close();
    68             } catch (SQLException e) {
    69                 // TODO Auto-generated catch block
    70                 e.printStackTrace();
    71                 throw new RuntimeException(e);
    72             }
    73         }
    74     }
    75   
    76 }

       工具类再重载一个带有3个参数的关闭连接的方法:

     1     public static void close(Connection conn,Statement stmt,ResultSet rs){
     2         if(rs!=null)
     3             try {
     4                 rs.close();
     5             } catch (SQLException e1) {
     6                 e1.printStackTrace();
     7                 throw new RuntimeException(e1);
     8             }
     9         if(stmt!=null){
    10             try {
    11                 stmt.close();
    12             } catch (SQLException e) {
    13                 e.printStackTrace();
    14                 throw new RuntimeException(e);
    15             }
    16         }
    17         if(conn!=null){
    18             try {
    19                 conn.close();
    20             } catch (SQLException e) {
    21                 e.printStackTrace();
    22                 throw new RuntimeException(e);
    23             }
    24         }
    25     }

    然后,1)jdbc使用DDL的方法要这样

     1 public class UtilTest {
     2 
     3     private static Connection conn = null;
     4     // 创建Statement对象
     5     private static Statement stsm;
     6 
     7     // 执行DDL语句(创建)
     8     private static void DDL() {
     9 
    10         try {
    11             // 使用jdbc工具类来获取连接对象
    12             conn = Jdbcutil.getConnection();
    13 
    14             // 准备sql语句
    15             String sql = "CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(10),sex VARCHAR(5),age INT,psot VARCHAR(10),email VARCHAR(20),phone INT)";
    16 
    17             stsm = conn.createStatement();
    18 
    19             // 发送sql语句
    20             int result = stsm.executeUpdate(sql);
    21         } catch (Exception e) {
    22             e.printStackTrace();
    23             throw new RuntimeException(e);
    24         } finally {
    25             // 调用工具类的方法,关闭连接
    26             Jdbcutil.close(conn, stsm);
    27         }
    28     }
    29     public static void main(String[] args) {
    30         DDL();
    31     }
    32 }

     

       

      2)执行DML语句:

     1     //创建驱动注册对象
     2     private static Connection conn = null;
     3     // 创建Statement对象
     4     private static Statement stsm = null;
     5 
     6 // 执行DML语句(插入)
     7     private static void DML() {
     8 
     9         try {
    10             // 使用工具类获取连接对象
    11             conn = Jdbcutil.getConnection();
    12 
    13             // 准备sql语句
    14             String sql = "INSERT INTO person (NAME,sex,age) VALUES ('张三','男',20);";
    15 
    16             // 创建statement对象
    17             stsm = conn.createStatement();
    18 
    19             // 执行sql语句
    20             int result = stsm.executeUpdate(sql);
    21             System.out.println("影响了" + result + "行");
    22 
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25             throw new RuntimeException(e);
    26         } finally {
    27             // 调用工具类关闭连接
    28             Jdbcutil.close(conn, stsm);
    29         }
    30     }

      3)执行DQL语句:

     1     // 创建驱动注册对象
     2     private static Connection conn = null;
     3     // 创建Statement对象
     4     private static Statement stsm = null;
     5 
     6         // 执行DQL语句
     7     private static void DQL() {
     8 
     9         try {
    10 
    11             // 调用工具类连接对象
    12             conn = Jdbcutil.getConnection();
    13 
    14             // 创建statement对象
    15             stsm = conn.createStatement();
    16 
    17             // 准备sql语句
    18             String sql = "SELECT * FROM person;";
    19 
    20             // 执行sql语句,返回的是RrsultSet对象
    21             ResultSet rs = stsm.executeQuery(sql);
    22 
    23             // 查看第二行数据
    24 
    25             // 移动光标
    26             rs.next();
    27             rs.next();
    28             // 使用列名来查看
    29             int id = rs.getInt("id");
    30             String name = rs.getString("name");
    31             String sex = rs.getString("sex");
    32             System.out.println(id + "," + name + "," + sex);
    33 
    34         } catch (Exception e) {
    35             e.printStackTrace();
    36             throw new RuntimeException(e);
    37         } finally {
    38             // 调用工具类关闭连接,这里要多关闭一个连接:ResultSet,工具类的关闭方法要添加它
    39             Jdbcutil.close(conn, stsm,rs);
    40         }
    41 
    42     }

      

  • 相关阅读:
    HashMap的存储原理
    HashSet的存储原理
    ArrayList的底层实现原理
    $.getJSON()不执行回调函数
    JavaScript学习笔记(一)
    【转】日语口语简略型总结(更新中。。。)
    计算机常用符号(日文)更新中。。。
    异常
    注解
    多线程
  • 原文地址:https://www.cnblogs.com/LZL-student/p/6012715.html
Copyright © 2011-2022 走看看