首页 > 数据库技术 > 详细

JDBC封装

时间:2019-08-02 19:49:17      阅读:140      评论:0      收藏:0      [点我收藏+]
//JDBC的封装
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
    private static String url=null;
    private static String user=null;
    private static String password=null;
    static {
        Properties properties = new Properties();
        try {
            properties.load(DBUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties"));
             String driver = properties.getProperty("driver");
             url = properties.getProperty("url");
             user = properties.getProperty("user");
             password = properties.getProperty("password");
             Class.forName(driver);
        
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {   
            e.printStackTrace();
        }
    }
    public static Connection getConn() {
        Connection conn =null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void close(ResultSet rs,Statement stm,Connection conn) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stm!=null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

//dbconfig.properties 配置文件

driver =com.mysql.jdbc.Driver
url =jdbc:mysql://localhost:3306/javaee1906
user=root
password=123789

JDBC封装

原文:https://www.cnblogs.com/cqzy/p/11290705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!