jdbc-获取连接的五种方式

package JDBCTest;

import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.*;

class ConnectionTestTest {
@Test
public void test5() throws Exception{
//final version
InputStream resourceAsStream = ConnectionTestTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
resourceAsStream.close();


}
@Test
public void test4() throws Exception{
String user = "root";
String password = "110120";
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
Class.forName("com.mysql.jdbc.Driver");//driver 有静态代码块 会自动加载

Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
@Test
public void test3() throws Exception{
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//获取driver的实例化对象

try {
DriverManager.registerDriver(driver);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String user = "root";
String password = "110120";
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
@Test
public void test2() throws Exception{
//获取方拾贰//为了不出现sun公司的第三方类
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//提供需要连接的数据库
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","110120");
Connection connect = driver.connect(url, info);
System.out.println(connect);
}

@Test
public void test1() throws SQLException {
//方式一
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","110120");
Connection connect = driver.connect(url, info);
System.out.println(connect);

}A

}
posted @   wiselee/  阅读(83)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示