JDBC连接方式

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;

import org.junit.Test;

public class MysqlTest {
	private String url="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC";
	private String user="root";
	private String password="root";
	
	public void test1() throws Exception{
		Driver driver=new com.mysql.cj.jdbc.Driver();
		Properties props=new Properties();
		props.setProperty("user",user);
		props.setProperty("password", password);
		Connection conn = driver.connect(url, props);
		System.out.println(conn);
	}
		
	public void test2() throws Exception{
		Driver driver2=new com.mysql.cj.jdbc.Driver();
		//注册驱动
		DriverManager.registerDriver(driver2);
		Connection conn=DriverManager.getConnection(url,user,password);
		System.out.println(conn);
	}
	
	public void test3() throws Exception{
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection conn=DriverManager.getConnection(url,user,password);
		System.out.println(conn);
	}
		
	public static void main(String[] args) throws Exception {
		MysqlTest d=new MysqlTest();
		d.test1();
		d.test2();
		d.test3();
	}
}

发布了25 篇原创文章 · 获赞 0 · 访问量 353

猜你喜欢

转载自blog.csdn.net/weixin_45808666/article/details/103596556