本文内容来自 尚硅谷JDBC核心技术 笔记部分。仅做个人总结

连接方式一

该方式代码中显式出现了第三方数据库的API

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

public class testConnection{
    public static void main(String[] args){
        testConnection1();
    }
    public static void testConnection1(){
        try{
        	// MySQK 8.0
        	// Driver driver = new com.mysql.cj.jdbc.Driver();
        	// MySQL 5.7
            Driver driver = new com.mysql.jdbc.Driver();

			// MySQK 8.0
        	// String url ="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"
        	// MySQL 5.7
            String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
连接方式二

相较于方式一,这里使用反射实例化Driver,不在代码中体现第三方数据库的API。体现了面向接口编程思想

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

public class testConnection{
    public static void main(String[] args){
        testConnection2();
    }
    public static void testConnection2(){
        try{
            String className = "com.mysql.jdbc.Driver";
            Class clazz = Class.forName(className);
            Driver driver = (Driver) clazz.newInstance();
            String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}

连接方式三

说明:使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。

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


public class testConnection{
    public static void main(String[] args){
        testConnection3();
    }
    public static void testConnection3() {
        try {
			// 四个基本要素
            String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
            String user = "root";
            String password = "root";
            String driverName = "com.mysql.jdbc.Driver";

            Class clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();

            DriverManager.registerDriver(driver);
            Connection conn = DriverManager.getConnection(url, user, password);

            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

提示:编译运行后,如果出现WARR:Establishing SSL connection without server’s identity verification is not recommended.
可以改为以下(红字为增加内容)。具体参考文章
jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false

连接方式四

说明:不必显式的注册驱动了。因为在DriverManager的源码中已经存在静态代码块,实现了驱动的注册。

import java.sql.Connection;
import java.sql.DriverManager;

public static void testConnection() {
	public static void main(String[] args){
        testConnection4();
    }
    public static void testConnection4() {
	     try{
	         //1.数据库连接的4个基本要素:
	         String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false";
	         String user="root";
	         String password="root";
	         String driverName="com.mysql.jdbc.Driver";
	
	         //2.加载驱动(①实例化Driver②注册驱动)
	         Class.forName(driverName);
	
	         //Driver driver=(Driver)clazz.newInstance();
	         // 3.注册驱动
	         // DriverManager.registerDriver(driver);
	         /*
	         可以注释掉上述代码的原因,是因为在mysql的Driver类中声明有:
	         static{
	             try{
	                 DriverManager.registerDriver(newDriver());
	             }catch(SQLException var1){
	                 throw new RuntimeException("Can't register driver!");
	             }
	         }
	         */
	
	         //3.获取连接
	         Connection conn=DriverManager.getConnection(url,user,password);
	         System.out.println(conn);
	     }catch(Exception e){
	         e.printStackTrace();
	     }
	}
}
连接方式五(最终版)

说明:使用配置文件的方式保存配置信息,在代码中加载配置文件
使用配置文件的好处:
①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码
②如果修改了配置信息,省去重新编译的过程

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

public class testConnection {
    public static void main(String[] args) {
        testConnection5();
    }
    public static void testConnection5(){
        try {
            //1.加载配置文件
            InputStream is=testConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pros=new Properties();
            pros.load(is);
            //2.读取配置信息
            String user=pros.getProperty("user");
            String password=pros.getProperty("password");
            String url=pros.getProperty("url");
            String driverClass=pros.getProperty("driverClass");
            //3.加载驱动
            Class.forName(driverClass);
            //4.获取连接
            Connection conn=DriverManager.getConnection(url,user,password);
            System.out.println(conn);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

其中,配置文件声明在工程的src目录下新建 jdbc.properties文件,添加以下内容

user=root
password=root
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
driverClass=com.mysql.jdbc.Driver

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐