JDBC_获取数据库连接

时间:2022-06-19 11:51:36
<span style="font-size:24px;">package src.com.jdbc.java;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;

public class JDBCTest {
@Test
public void testGetConnection2() throws Exception{
System.out.println(getConnection2());
}
public Connection getConnection2() throws Exception{
//1、准备连接数据库的四个字符
//1)、创建properties对象
Properties properties=new Properties();

//2)、获取jdbc.properties对象
InputStream in=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

//3)、加载2)对应的输入流
properties.load(in);
//4)、具体决定user,passwrod等四个字符串
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String jdbcUrl=properties.getProperty("jdbcUrl");
String driver=properties.getProperty("driver");

//2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driver);
//3、通过DriverManager的getConnection()方法获取数据库的连接。
return (Connection) DriverManager.getConnection(jdbcUrl,user,password);
}
/**
* DriverManager 是驱动的管理类.
* 1、可以通过重载的getconnection()方法获取连接,
* 2、可以同时管理多个驱动程序;注册了多个数据库连接,则调用getconnection()方法
* 时传入的参数不同,即返回不同的数据库连接
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*
*/
@Test
public void testDriverManager() throws Exception{
//1、准备连接数据库的四个字符
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取类路径下jdbc.properties
InputStream in = getClass().getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//2、加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driverClass);
//3、通过DriverManager的getConnection()方法获取数据库的连接。
Connection connection=(Connection) DriverManager.getConnection(jdbcUrl,user,password);
System.out.println(connection);
}
@Test
public void testDriver() throws SQLException {
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://127.0.0.1:3306/cjl";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "root123");
Connection connection = (Connection) driver.connect(url, info);
System.out.println(connection);

}

/**
* 编写一个通用方法:把数据库驱动Driver实现类的全类名、URL、user、password放入
* 一个配置文件中,通过修改配置文件的方式实现和具体的数据库解偶
*
* @throws Exception
*/
public Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取类路径下jdbc.properties
InputStream in = getClass().getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//通过反射创建Driver对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();

Properties info = new Properties();
info.put("user", user);
info.put("password", password);
//通过Driver的connection方法获取数据库连接
Connection connection = (Connection) driver.connect(jdbcUrl, info);
return connection;
}
@Test
public void testGetConnection() throws Exception{
System.out.println(getConnection());
}

}
</span>

jdbc.properties文件中的配置

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/cjl
user=root
password=root123