package jdbc; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class ConnectionTest { //方式1: @Test public void testConnection1() throws SQLException { //获取Driver实现类对象 Driver driver = new com.mysql.cj.jdbc.Driver(); //jdbc:mysql:协议 //localhost:ip地址 //3306:默认mysql的端口号 //test:test数据库 String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; //将用户名和密码封装在Properties中 Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","2003314w"); Connection conn = driver.connect(url,info); System.out.println(conn); } //方式2:对方法1的迭代,在如下的程序中不出现第三方的api,使得程序具有更好的可移植性 @Test public void testConnection2() throws Exception { //1.获取Driver实现类对象 使用反射 Class clazz = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); //2.提供要连接的数据库 String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; //3.提供连接需要的用户名和密码 Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","2003314w"); //4.获取连接 Connection connect = driver.connect(url, info); System.out.println(connect); } //方法3.使用DriverManager替换Driver @Test public void testConnection3() throws Exception { //1.获取Driver实现类对象 Class clazz = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); //2.提供另外三个连接的基本信息 String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; String user = "root"; String password = "2003314w"; //3.注册驱动 DriverManager.registerDriver(driver); //4.获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } //方法4:可以只是加载驱动,不用显示的注册驱动了 @Test public void testConnection4() throws Exception { //1.提供三个连接的基本信息 String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; String user = "root"; String password = "2003314w"; //2.获取Driver实现类对象 Class.forName("com.mysql.cj.jdbc.Driver"); //相较于方式3,可以省略如下操作 // Driver driver = (Driver) clazz.newInstance(); // // 注册驱动 // DriverManager.registerDriver(driver); //3.获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } //方式5(final版):将数据库连接的四个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接 /* 此种方式的好处? 1.实现了数据与代码的分离。实现了解耦 2.如果需要修改配置文件信息,可以避免程序重新打包 */ @Test public void getConnection5() throws Exception { //1.读取配置文件的4个基本信息 InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); String driverClass = properties.getProperty("driverClass"); //2.加载驱动 Class.forName(driverClass); //3.获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } }
05-29
04-30
1134