【JDBC】工具类的抽取

jdbc.properties属性文件

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///jdbctest
username=root
password=1234

JDBCUtils.java工具类

package com.imooc.jdbc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * JDBC的工具类
 * @author jt
 *
 */
public class JDBCUtils {
    private static final String driverClass;
    private static final String url;
    private static final String username;
    private static final String password;
    
    static{
        // 加载属性文件并解析:
        Properties props = new Properties();
        // 如何获得属性文件的输入流?
        // 通常情况下使用类的加载器的方式进行获取:
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        driverClass = props.getProperty("driverClass");
        url = props.getProperty("url");
        username = props.getProperty("username");
        password = props.getProperty("password");
    }

    /**
     * 注册驱动的方法
     * @throws ClassNotFoundException 
     */
    public static void loadDriver() throws ClassNotFoundException{
        Class.forName(driverClass);
    }
    
    /**
     * 获得连接的方法:
     * @throws SQLException 
     */
    public static Connection getConnection() throws Exception{
        loadDriver();
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }
    
    /**
     * 资源释放
     */
    public static void release(Statement stmt,Connection conn){
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        if(rs!= null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

JDBCDemo3.java测试

package com.imooc.jdbc.demo1;

import java.sql.Connection;
import java.sql.Statement;

import org.junit.Test;

import com.imooc.jdbc.utils.JDBCUtils;

public class JDBCDemo3 {

    @Test
    // 保存记录
    public void demo1(){
        Connection conn = null;
        Statement stmt  = null;
        try{
            // 获得连接:
            conn = JDBCUtils.getConnection();
            // 创建执行SQL语句的对象
            stmt = conn.createStatement();
            // 编写SQL:
            String sql = "insert into user values (null,'ggg','123','小六')";
            // 执行SQL:
            int num = stmt.executeUpdate(sql);
            if(num > 0){
                System.out.println("保存成功!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            // 释放资源:
            JDBCUtils.release(stmt, conn);
        }
    }
}

你可能感兴趣的:(【JDBC】工具类的抽取)