码迷,mamicode.com
首页 > 数据库 > 详细

【JDBC】工具类的抽取

时间:2019-10-05 00:56:19      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:out   password   pack   import   etc   input   编写   资源释放   stream   

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】工具类的抽取

标签:out   password   pack   import   etc   input   编写   资源释放   stream   

原文地址:https://www.cnblogs.com/huowuyan/p/11623737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!