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
}
本文来自博客园,作者:wiselee/,转载请注明原文链接:https://www.cnblogs.com/wiseleer/articles/15940421.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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框架的用法!