“使用JDBCUtils”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf?p=14”的新页面)
 
 
第1行: 第1行:
https://www.bilibili.com/video/BV1eJ411c7rf?p=14
+
https://www.bilibili.com/video/BV1eJ411c7rf?p=14<syntaxhighlight lang="java">
 +
import java.io.InputStream;
 +
import java.sql.Connection;
 +
import java.sql.DriverManager;
 +
import java.sql.SQLException;
 +
import java.sql.Statement;
 +
import java.util.Properties;
 +
 
 +
public class JDBCUtils {
 +
 
 +
    /**
 +
    * 获取数据库的连接
 +
    * @return
 +
    * @throws Exception
 +
    */
 +
    public static Connection getConnection() throws Exception {
 +
 
 +
        //1.读取配置文件中的4个基本信息
 +
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
 +
 
 +
        Properties pros = new Properties();
 +
 
 +
        pros.load(is);
 +
 
 +
        String user = pros.getProperty("user");
 +
 
 +
        String password = pros.getProperty("password");
 +
 
 +
        String url = pros.getProperty("url");
 +
 
 +
        String driverClass = pros.getProperty("driverClass");
 +
 
 +
        //2.加载驱动
 +
        Class.forName(driverClass);
 +
 
 +
        //3.获取连接
 +
        Connection conn = DriverManager.getConnection(url, user, password);
 +
 
 +
        return conn;
 +
    }
 +
 
 +
    /**
 +
    * 关闭连接和 Statement 的操作
 +
    * @param conn
 +
    * @param ps
 +
    */
 +
    public void closeResource(Connection conn, Statement ps){
 +
        try {
 +
            if (ps != null) {
 +
                ps.close();
 +
            }
 +
        } catch (SQLException e) {
 +
            throw new RuntimeException(e);
 +
        }
 +
 
 +
        if (conn != null){
 +
            try {
 +
                conn.close();
 +
            } catch (SQLException e) {
 +
                throw new RuntimeException(e);
 +
            }
 +
        }
 +
    }
 +
 
 +
}
 +
</syntaxhighlight>https://github.com/jihch/jdbc/blob/main/src/main/java/io/github/jihch/util/JDBCUtils.java

2022年12月16日 (五) 12:50的最新版本

https://www.bilibili.com/video/BV1eJ411c7rf?p=14

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {

    /**
     * 获取数据库的连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {

        //1.读取配置文件中的4个基本信息
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();

        pros.load(is);

        String user = pros.getProperty("user");

        String password = pros.getProperty("password");

        String url = pros.getProperty("url");

        String driverClass = pros.getProperty("driverClass");

        //2.加载驱动
        Class.forName(driverClass);

        //3.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;
    }

    /**
     * 关闭连接和 Statement 的操作
     * @param conn
     * @param ps
     */
    public void closeResource(Connection conn, Statement ps){
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

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

}

https://github.com/jihch/jdbc/blob/main/src/main/java/io/github/jihch/util/JDBCUtils.java