JDBCutils

通过上一篇文章知道了Java封装的JDBC类库用来连接各种数据库
但是其中还是有许多繁琐冗余的代码需要编写,因此,在上一篇文章的基础上,继续封装

JDBCutils类:

package study.jdbcutils;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCutils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     * 静态代码块
     * 静态代码块中的代码在类加载时只运行一次
     *
     * 读取配置文件,给对象属性赋值
     *
     * NOTE:
     * 1、静态代码块中的属性必须是静态
     * 2、静态代码块中的异常必须捕获,不能抛出
     */
    static{
        try {
            //读取资源文件 获取值
            //创建Properties集合
            Properties pro = new Properties();

            //获取配置文件的绝对路径
            ClassLoader classLoader = JDBCutils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");

            String path = res.getPath();
            //加载文件进内存
            pro.load(new FileReader(path));

            //获取属性,并且赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            //注册驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }


    /**
     * 获取连接对象
     * @return Connection
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }


    /**
     * 关闭资源
     * @param pstm
     * @param conn
     */
    public static void colse(PreparedStatement pstm, Connection conn)
    {
        if (pstm != null){
            try {
                pstm.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 关闭资源
     * @param res
     * @param pstm
     * @param conn
     */
    public static void colse(ResultSet res,PreparedStatement pstm, Connection conn)
    {
        if (res != null){
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pstm != null){
            try {
                pstm.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

Demo类:

package study.jdbcutils;

import study.jdbc.Emp;

import java.sql.*;

public class Jdbc {
    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet res = null;
        PreparedStatement pstm = null;
        try {
            //使用工具类
            conn = JDBCutils.getConnection();

            //定义查询语句 使用?占位符
            String select = "select * from emp where id = ?";

            //获取执行SQL对象
            pstm = conn.prepareStatement(select);
            //占位符参数
            pstm.setInt(1,1002);
            //执行查询语句
            res = pstm.executeQuery();

            while (res.next()){
                System.out.println(res.getInt("id"));
                System.out.println(res.getString("ename"));
                System.out.println(res.getInt("job_id"));
                System.out.println(res.getInt("mgr"));
                System.out.println(res.getDate("joindate"));
                System.out.println(res.getDouble("salary"));
                System.out.println(res.getDouble("bonus"));
                System.out.println(res.getInt("dept_id"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            JDBCutils.colse(res,pstm,conn);
        }
    }
}

配置文件(jdbc.properties):

url=jdbc:mysql://127.0.0.1:3306/test
user=root
password=root
driver=com.mysql.jdbc.Driver

NOTE:
静态代码块的使用
配置文件的读取
PreparedStatement对象完成SQL的参数绑定,防止SQL注入

仅有一条评论

添加新评论