zoukankan      html  css  js  c++  java
  • JDBC工具类

    一:工具类  方便他人进行调用。

    工具类一般不需要对象,进行静态调用。所以构造方法进行私有化。

     1 package day31;
     2 
     3 import java.sql.*;
     4 
     5 public class jdbcutils {
     6     /*
     7     创建jdbc工具类。
     8     1:方便别人调用
     9     2:避免代码重复。
    10      */
    11     private jdbcutils(){}//工具类不需要实例化,所以方法进行私有化。
    12     private static Connection con;//需要静态变量
    13 
    14     /*
    15     静态代码块在加载类的时候就执行该部分的代码。
    16      */
    17     static {
    18         try{
    19             Class.forName("com.mysql.jdbc.Driver");
    20             String url="jdbc:mysql://192.168.147.146:3306/homework_day13";
    21             String  username="test";
    22             String  password="123456";
    23             con= DriverManager.getConnection(url,username,password);
    24         }catch (Exception ex){
    25             throw  new RuntimeException(ex+"数据库连接失败!");//如果出现异常的话  需要种子程序 所以要抛出异常。需要创建运行异常的错误。
    26         }
    27     }
    28 
    29     public  static Connection  getCon(){
    30         return  con;
    31     }
    32     /*
    33     关闭资源。
    34     通过方法的重载来判断用户执行的是查询和更新操作。
    35      */
    36     public  static   void  cls_re (Connection con, Statement pst, ResultSet rs)throws SQLException{
    37         /*
    38         注意:这里需要判断需要关闭的对象是否存在以及该对象如果抛出异常不能影响下面的关闭。
    39         这里是Statement  是prepareStament的父类。
    40          */
    41         if(con!=null){
    42             try {
    43                 con.close();
    44             }catch (Exception ex){}
    45             }
    46             if(pst!=null){
    47             try {
    48                 pst.close();
    49             }catch (Exception ex){}
    50             }
    51             if(rs!=null){
    52             try {
    53                 rs.close();
    54             }catch (Exception ex){}
    55             }
    56 
    57     }
    58     public static  void  cls_re(Connection con,Statement pst){
    59         if(pst!=null){
    60             try {
    61                 pst.close();
    62             }catch (Exception ex){}
    63         }
    64         if(pst!=null){
    65             try {
    66                 pst.close();
    67             }catch (Exception ex){}
    68         }
    69     }
    70 }

     调用:

     1 package day31;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 
     8 public class testjdbc {
     9     public  static  void  main(String[] args)throws SQLException{
    10         Connection con =jdbcutils.getCon();
    11         PreparedStatement pst=con.prepareStatement("select * from system_user");
    12         ResultSet  ret=pst.executeQuery();
    13         while (ret.next()){
    14             System.out.print(ret.getString("username")+" "+ret.getString("password"));
    15         }
    16         jdbcutils.cls_re(con,pst,ret);
    17     }
    18 }
  • 相关阅读:
    使用 Dockerfile 定制镜像
    UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
    UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
    LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
    LeetCode Number of Islands 岛的数量(DFS,BFS)
    LeetCode Triangle 三角形(最短路)
    LeetCode Swap Nodes in Pairs 交换结点对(单链表)
    LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
    HDU 5312 Sequence (规律题)
    LeetCode Letter Combinations of a Phone Number 电话号码组合
  • 原文地址:https://www.cnblogs.com/evilliu/p/8462317.html
Copyright © 2011-2022 走看看