一般JDBC的常见写法(附懒汉式 && 饿汉式)

首先在建个DbUtil文件,里面返回Connection、关闭方法等,如下所示:
[java] view plain copy
  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. /** 
  8.  * 数据库工具类(获取连接、释放资源) 
  9.  *  
  10.  * @author YUSHIBUJUE 
  11.  *  
  12.  */  
  13. public abstract class DbUtil {  
  14.       
  15.     // 只需要执行一次  
  16.     static{  
  17.         try {  
  18.             System.out.println("注册数据库驱动......");  
  19.             Class.forName("com.mysql.jdbc.Driver");  
  20.         } catch (ClassNotFoundException e) {  
  21.             throw new RuntimeException("注册数据库驱动出现异常:"+e.getMessage());  
  22.         }  
  23.     }  
  24.     /** 
  25.      * 获取一个连接对象 
  26.      * @return 
  27.      */  
  28.     public static Connection getConn() {  
  29.         String url = "jdbc:mysql://localhost/flightsystem?characterEncoding=GBK";  
  30.         try {  
  31.             return DriverManager.getConnection(url, "root""root");  
  32.         } catch (SQLException ex1) {  
  33.             throw new RuntimeException("无法获取连接,原因:"+ex1.getMessage());  
  34.         }  
  35.     }  
  36.       
  37.       
  38.     public static void free(ResultSet rs, Statement st, Connection conn) {  
  39.         try {  
  40.             if (null != rs) {  
  41.                 rs.close();  
  42.                 System.out.println("ResultSet已关闭!");  
  43.             }  
  44.         } catch (SQLException e) {  
  45.             throw new RuntimeException("关闭ResultSet出现异常:" + e.getMessage());  
  46.         } finally {  
  47.             try {  
  48.                 if (null != st) {  
  49.                     st.close();  
  50.                     System.out.println("Statement已关闭!");  
  51.                 }  
  52.             } catch (SQLException e) {  
  53.                 throw new RuntimeException("关闭Statement出现异常:" + e.getMessage());  
  54.             } finally {  
  55.                 if (null != conn) {  
  56.                     try {  
  57.                         conn.close();  
  58.                         System.out.println("Connection已关闭!");  
  59.                     } catch (SQLException e) {  
  60.                         throw new RuntimeException("关闭Connection出现异常:"  
  61.                                 + e.getMessage());  
  62.                     }  
  63.                 }  
  64.             }  
  65.         }  
  66.   
  67.     }  
  68. }  



接着在使用JDBC的地方,调用free(),如下:

一般JDBC的常见写法(附懒汉式 && 饿汉式)_第1张图片



原文出处:http://blog.csdn.net/xuan6251237011/article/details/12089311




懒汉式

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {
    private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    private static final String DBURL = "jdbc:mysql://localhost:3306/test";
    private static final String DBUSER = "root";
    private static final String PASSWORD = "admin";
    private Connection conn = null;

    // 如果要实例化本类对象,实际上就是为了取得数据库连接,那么就把连接过程交给构造方法
    public DatabaseConnection() {
        try {
            Class.forName(DBDRIVER);
            this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return this.conn;
    }

    public void close() {
        if (this.conn != null) {
            try { // 关闭时几乎不会有问题
                this.conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}





饿汉式

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataBaseConnection {
    private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    private static final String DBURL = "jdbc:mysql://localhost:3306/test";
    private static final String DBUSER = "root";
    private static final String PASSWORD = "admin";
    public Connection conn;

    //饿汉式数据库连接
    public Connection getConnection() {
        try {
            if (conn == null && conn.isClosed())
                Class.forName(DBDRIVER);
            conn = DriverManager.getConnection(DBURL, DBUSER, PASSWORD);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    public void close(){
        if(conn != null){
            try {
                this.conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(Java)