文章目录
数据库连接字符串url: 主协议 : 子协议 : ip地址 : 数据库端口号 : 数据库名
一. 获取连接Connection
硬编码
方法1:硬编码 - Driver
public static Connection getConnection1() throws SQLException {
// 1. 加载驱动
Driver driver = new oracle.jdbc.driver.OracleDriver();
// 2. 连接数据库
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
// 属性对象--连接字符串需要的一些信息 封装到 一个Properties对象上
Properties info = new Properties();
info.setProperty("user", "scott");
info.setProperty("password", "123456");
Connection conn = driver.connect(url, info);
System.out.println(conn);
// 3. 返回Connection对象
return conn;
}
反射、硬编码
方法2:反射、硬编码结合 - Class、Driver、DriverManager
@Test
public static Connection getConnection3() throws Exception {
Class class_driver = Class.forName("oracle.jdbc.driver.OracleDriver");
Driver driver = (Driver) class_driver.newInstance();
// 1. 注册新驱动
DriverManager.registerDriver(driver);
// 2. 连接数据库的信息
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "123456";
// 3. 连接数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
return conn;
}
方法3:反射、硬编码结合 - Class、Driver
public static Connection getConnection2() throws Exception {
// 1. 使用反射获取数据库驱动对象
Class<?> class_driver = Class.forName("oracle.jdbc.driver.OracleDriver");
Driver driver = (Driver) class_driver.newInstance();
// 2. 数据库连接字符串 主协议:子协议:ip地址:数据库端口号:数据库名
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
// 3. 属性对象--连接字符串需要的一些信息 封装到 一个Properties对象上
Properties info = new Properties();
info.setProperty("user", "scott");
info.setProperty("password", "123456");
// 4. 连接数据库
Connection conn = driver.connect(url, info);
System.out.println(conn);
return conn;
}
方法4:反射、硬编码结合 - Class、DriverManager
@Test
public static Connection getConnection4() throws Exception {
// 1.加载类,静态代码块自动注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. 连接数据库的信息
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "123456";
// 3. 连接数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
return conn;
}
反射、配置文件 – 最终版
方法5:反射、配置文件结合 - Class、FileInputStream
public static Connection getConnection5() throws Exception {
// InputStream is = TestConnection.class.getClassLoader().getResourceAsStream("E:/eclipse/workspace/东软/src/top/linruchang/jdbc/jdbc.properties");
// 1. 读取连接数据库 配置信息 从文件中
InputStream is = new FileInputStream("E:/eclipse/workspace/东软/src/top/linruchang/jdbc/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);
System.out.println(conn);
return conn;
}
二. 关闭资源
public static void closeResource(Connection conn, Statement statement, ResultSet rs) {
// 1. 关闭结果集
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 2. 关闭sql语句声明
try {
if (statement != null)
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 3. 关闭数据库连接
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}