欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

封装jdbc 博客分类: java  

程序员文章站 2024-03-24 11:20:52
...
1.定义实体类
package com.wangxuegang.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcObject {
	
	private Connection connection; //获取连接
	private PreparedStatement preStatement; //执行器
	private ResultSet resultSet; //返回结果
	
	public Connection getConnection() {
		return connection;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	public PreparedStatement getPreStatement() {
		return preStatement;
	}
	public void setPreStatement(PreparedStatement preStatement) {
		this.preStatement = preStatement;
	}
	public ResultSet getResultSet() {
		return resultSet;
	}
	public void setResultSet(ResultSet resultSet) {
		this.resultSet = resultSet;
	}
}

2.jdbc工具类
package com.wangxuegang.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import com.wangxuegang.model.JdbcObject;

/**
 * 
 * @description JDBC工具类
 * @author wangxuegang
 * @since JDK 1.6
 */
public final class JDBCUtils {
	
    private static final String DRIVER; //驱动
    private static final String URL; //连接数据库地址
    private static final String USER; //用户名
    private static final String PASSWORD; //密码
    
    private JDBCUtils(){}
    
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        
        DRIVER = bundle.getString("driver");
        URL = bundle.getString("url");
        USER = bundle.getString("user");
        PASSWORD = bundle.getString("password");
        
        /**
         * 驱动注册
         */
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    /**
     * 获取 Connetion
     */
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
    
    /**
     * 释放资源
     */
    public static void colseResource(Connection conn,Statement st,ResultSet rs) {
        closeResultSet(rs);
        closeStatement(st);
        closeConnection(conn);
    }
    
    /**
     * 释放连接 Connection
     */
    public static void closeConnection(Connection conn) {
        if(conn !=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //等待垃圾回收
        conn = null;
    }
    
    /**
     * 释放语句执行者 Statement
     */
    public static void closeStatement(Statement st) {
        if(st !=null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //等待垃圾回收
        st = null;
    }
    
    /**
     * 释放结果集 ResultSet
     */
    public static void closeResultSet(ResultSet rs) {
        if(rs !=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //等待垃圾回收
        rs = null;
    }
    
    /**
	 * 
	 * @description 获取连接,传入SQL
	 */
	public static JdbcObject execute(JdbcObject object,String sql) throws SQLException{
		object.setConnection(JDBCUtils.getConnection());
		object.setPreStatement(object.getConnection().prepareStatement(sql));
		return object;
	}
	
	/**
	 * 
	 * @description 关闭资源
	 */
	public static void closeJdbcObject(JdbcObject object){
		JDBCUtils.colseResource(object.getConnection(),object.getPreStatement(),object.getResultSet());
	}
}

 

3.数据库配置jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root