JDBC的封装

JDBC是Java连接数据库的关键技术,所以必须要使用,但是每一次要使用都要重写8个基本步骤太繁琐,所以这边可以选择封装JDBC进行调用,把一些重复要写的代码放入静态代码块中,随着类的加载而执行,并且只执行一次。
首先在根目录下新建db.properities文件
把链接mysql的基本数据写入db.properities中,因为是连接的mysql数据库所以driver是这种写法,如果是oracle数据库就是不同的写法,所以不同的数据库在写法上大同小异。

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

新建工具类DBUtils,把JDBC封装起来,方便调用。

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

    static {
        try {
            Properties properties = new Properties();
            ClassLoader classLoader = DBUtills.class.getClassLoader();
            URL res = classLoader.getResource("db.properties");
            String path = res.getPath();
            properties.load(new FileReader(path));
            //获取数据
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
            System.out.println(url + "  " + password + "  " + user);
            Class.forName(driver);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(Statement stat, Connection conn) {
        if( stat != null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if( conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement stat, Connection conn, ResultSet rs ) {
        if( rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if( stat != null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

    public static void main(String[] args) {
        try {
            Connection conn = DBUtills.getConn();
            if (conn != null) {
                System.out.println("连接成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值