JDBC : Java获取数据库连接(Driver And DriverManager)

时间:2022-03-08 02:58:16
Driver:
package xuezaipiao;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.Driver;import java.sql.SQLException;import java.util.Properties;public class getDabseConnection {	public static void main(String[] args){				System.out.println(getConnection());			}	private static Connection getConnection(){				//1.获取本地配置文件				Properties properties = new Properties();		try {			properties.load(new FileInputStream("D://LearnJava//learnJDBC//src//jdbc.properties"));		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}		String driverName = properties.getProperty("driver");		//2.创建数据库驱动				Driver driver = null;		try {			driver = (Driver) Class.forName(driverName).newInstance();//反射创建		} catch (InstantiationException e) {			e.printStackTrace();		} catch (IllegalAccessException e) {			e.printStackTrace();		} catch (ClassNotFoundException e) {			e.printStackTrace();		}		//3.获取配置信息				String user = properties.getProperty("user");		String password = properties.getProperty("password");		String jdbcUrl = properties.getProperty("jdbcUrl");		Properties info = new Properties();		info.put("user", user);		info.put("password", password);				//4.数据库连接				Connection connection = null;		try {			connection = driver.connect(jdbcUrl, info);		} catch (SQLException e) {			e.printStackTrace();		}				return connection;	}}


jdbc.properties

#oracle
driver=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
password=qiaolezi

#MySQL
#driverClass=com.mysql.jdbc.Driver
#jdbcUrl=jdbc:mysql://127.0.0.1:3306/test
#user=root
#password=qiaolezi

只需改动properties就可以获取到不同的数据库连接

当然Driver是最原始的,还可以通过DriverManager获取数据库连接

package xuezaipiao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class getDabseConnection {

public static void main(String[] args) throws Exception{

//System.out.println(getDriverConnection());
System.out.println(getDriverManagerCon());

}
private static Connection getDriverManagerCon() throws Exception{

//1.获取配置文件信息

Properties properties = new Properties();
try {
properties.load(new FileInputStream("D://LearnJava//learnJDBC//src//jdbc.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String dirverName = properties.getProperty("driver");

//2.加载数据库驱动程序(注册驱动),因为对于的Driver实现类中有注册驱动的静态代码块(看源代码)

try {
//DriverManager.registerDriver((Driver) Class.forName(dirverName).newInstance());
Class.forName(dirverName);//实际上应该写成注释部分,但是这样也可以
} catch (ClassNotFoundException e1) {

e1.printStackTrace();
}

//3.通过getConnection()获取数据库连接

Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcUrl, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

}

Driver源码

JDBC : Java获取数据库连接(Driver And DriverManager)

DriverManger的好处:

DriverManager是驱动管理类

1)可以通过重载的getConnection()方法获取数据库连接,相比较Driver而言比较方便

2)可以同时管理多个驱动程序,只需要getConnection()方法中的参数不同即可,而Driver需要Properties

不同数据库连接形式:

对于 Oracle 数据库连接,采用如下形式: 
jdbc:oracle:thin:@localhost:1521:sid
对于 SQLServer 数据库连接,采用如下形式:
jdbc:microsoft:sqlserver//localhost:1433:sid
对于 MYSQL 数据库连接,采用如下形式:   
jdbc:mysql://localhost:3306/sid

PS:sid是数据库名