一:五种连接方式 直接上码
package com.wyjedu.jdbc;
import com.mysql.jdbc.Driver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 测试连接方式
*/
public class Demo_jdbc02 {
public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
//测试连接方式一:
connect01();
//测试连接方式二:
connect02();
//测试连接方式三:
connect03();
//测试连接方式四:
connect04();
//测试连接方式五
connect05();
}
//1.第一种连接方式 直接创建driver 对象
public static void connect01() throws SQLException {
//(1):注册驱动:
Driver driver = new Driver();
//(2):得到连接
String url = "jdbc:mysql://localhost:3306/my_jdbc";
Properties properties = new Properties();
//获取用户 密码
properties.setProperty("user","root");
properties.setProperty("password","wyj");
//开始连接
Connection connect = driver.connect(url, properties);
System.out.println("连接方式一 = " + connect);
connect.close();
}
// 第二种连接方式 通过反射来连接
public static void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//(1):注册驱动
//反射加载Driver类,创建它的Class对象,动态加载,更加灵活,减少依赖性
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
// 实例化aclass的一个对象
Driver driver = (Driver) aClass.newInstance();
//(2):得到连接
String url = "jdbc:mysql://localhost:3306/my_jdbc";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","wyj");
Connection connect = driver.connect(url, properties);
System.out.println("方式二 = " + connect);
connect.close();
}
//第三种连接方式 使用DriverManger 替代 driver 进行统一管理
public static void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//(1):注册驱动
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//注册驱动 driver
DriverManager.registerDriver(driver);
//(2):得到连接
String url = "jdbc:mysql://localhost:3306/my_jdbc";
//可以 不用创建 property
String user = "root";
String password = "wyj";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第三种连接方式 = " + connection);
}
//4.使用 Class.forName 自动完成注册驱动,(其driver 源码当中有解释)
public static void connect04() throws ClassNotFoundException, SQLException {
//(1):注册驱动
Class.forName("com.mysql.jdbc.Driver");
/**
* Driver
* 源码:static {
* try {
* 注意这里是和方式三当中我们写的是一样的,也就是自动完成注册
* DriverManager.registerDriver(new Driver());
* } catch (SQLException var1) {
* throw new RuntimeException("Can't register driver!");
* }
* }
*
*/
//(2):得到连接
String url = "jdbc:mysql://localhost:3306/my_jdbc";
String user = "root";
String password = "wyj";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第四种连接方式 = " + connection);
connection.close();
}
//5.DriverManager getConnection ("jdbc: mysql: //localhost: 3306/testdb","root
//"root");中的字符串各个值,比如端口,数据库,用户名,密码为了方便,我们可以将信息
//写入到 properties文件中,方便操作
//这样在连接不同的数据库时,可以直接修改配置文件,而不用修改源码
public static void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过Properties获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src//mysql.properitys"));
//获取配置文件的相关信息
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
//(1):注册驱动(可以不用写 在导入的jar包下 有相关的配置文件信息 会自动加载,但你一般还是会写上 这样可以清楚知道,注册是谁的数据库)
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("连接方式五 = "+connection);
connection.close();
}
}
如有疑问,请留言