“获取数据库连接的方式五”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第2行: | 第2行: | ||
将连接数据库要用到的基本信息外部化到配置文件中,通过读取配置文件的方式获取连接 | 将连接数据库要用到的基本信息外部化到配置文件中,通过读取配置文件的方式获取连接 | ||
+ | |||
+ | |||
+ | 外部化的配置文件:resources\jdbc.properties<syntaxhighlight lang="properties"> | ||
+ | user=root | ||
+ | password=123456 | ||
+ | url=jdbc:mysql://localhost:3306/guns | ||
+ | driverClass=com.mysql.cj.jdbc.Driver | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | import org.junit.Test; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | import java.io.InputStream; | ||
+ | import java.sql.Connection; | ||
+ | import java.sql.DriverManager; | ||
+ | import java.sql.SQLException; | ||
+ | import java.util.Properties; | ||
+ | |||
+ | public class ConnectionTest5 { | ||
+ | |||
+ | @Test | ||
+ | public void testConnection() throws SQLException, ClassNotFoundException, IOException { | ||
+ | |||
+ | //1.读取配置文件中的4个基本信息 | ||
+ | InputStream is = ConnectionTest5.class.getClassLoader().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"); | ||
+ | |||
+ | System.out.println(user); | ||
+ | |||
+ | System.out.println(password); | ||
+ | |||
+ | System.out.println(url); | ||
+ | |||
+ | System.out.println(driverClass); | ||
+ | |||
+ | //2.加载驱动 | ||
+ | Class.forName(driverClass); | ||
+ | |||
+ | //3.获取连接 | ||
+ | Connection conn = DriverManager.getConnection(url, user, password); | ||
+ | |||
+ | System.out.println(conn); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | root | ||
+ | 123456 | ||
+ | jdbc:mysql://localhost:3306/guns | ||
+ | com.mysql.cj.jdbc.Driver | ||
+ | com.mysql.cj.jdbc.ConnectionImpl@147ed70f | ||
+ | </syntaxhighlight> |
2022年12月14日 (三) 09:26的版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=11
将连接数据库要用到的基本信息外部化到配置文件中,通过读取配置文件的方式获取连接
外部化的配置文件:resources\jdbc.properties
user=root
password=123456
url=jdbc:mysql://localhost:3306/guns
driverClass=com.mysql.cj.jdbc.Driver
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest5 {
@Test
public void testConnection() throws SQLException, ClassNotFoundException, IOException {
//1.读取配置文件中的4个基本信息
InputStream is = ConnectionTest5.class.getClassLoader().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");
System.out.println(user);
System.out.println(password);
System.out.println(url);
System.out.println(driverClass);
//2.加载驱动
Class.forName(driverClass);
//3.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
root
123456
jdbc:mysql://localhost:3306/guns
com.mysql.cj.jdbc.Driver
com.mysql.cj.jdbc.ConnectionImpl@147ed70f