常见数据库连接方式有哪些?
以Java操作mysql为例 //方式一
public void connect01() throws SQLException {
Driver driver = new Driver();
String url = 'jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false';
//将用户名和密码放入到Properties 对象
Properties properties = new Properties();
//说明user和password 是规定好,后面的值根据实际情况写
properties.setProperty('user', 'root');//用户
properties.setProperty('password', '123456');//密码
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
//方式二
```@Test
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//使用反射加载Driver类,动态加载,更加灵活,减少依赖性
Class aClass = Class.forName('com.mysql.cj.jdbc.Driver');
Driver driver = (Driver) aClass.newInstance();
String url = 'jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false';
//将用户名和密码放入到Properties 对象
Properties properties = new Properties();
//说明user和password 是规定好,后面的值根据实际情况写
properties.setProperty('user', 'root');//用户
properties.setProperty('password', '123456');//密码
Connection connect = driver.connect(url, properties);
System.out.println('方式二:'+connect);
}
//方式3 使用DriverManager 替代 Driver 进行统一管理
```@Test
public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//使用反射加载Driver
Class aClass = Class.forName('com.mysql.cj.jdbc.Driver');
Driver driver =(Driver) aClass.newInstance();
//创建url 和 user 和 password
String url = 'jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false';
String user = 'root';
String password = '123456';
DriverManager.registerDriver(driver);//注册driver驱动
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println('第三种方式' + connection);
}
//方式4 使用Class.forName 自动完成注册驱动,简化代码
//这种方式获取连接推荐使用
```@Test
public void connect04() throws ClassNotFoundException, SQLException {
//使用反射加载了Driver类
//在加载Driver类时,完成注册
//为什么使用反射的时候不用注册加载驱动类了,因为底层已经帮我们注册好了
/*
源码:1.静态代码块,在类加载时,会执行一次
2.DriverManager.registerDriver(new Driver())
3.因此注册driver的工作已经完成
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException('Can't register driver!');
}
}
*/
Class.forName('com.mysql.cj.jdbc.Driver');
// com.mysql.cj.jdbc.Driver //创建url user 和password String url = 'jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false'; String user = 'root'; String password = '123456'; Connection connection = DriverManager.getConnection(url, user, password); System.out.println('第四种方式'+connection); }
//方式5 在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
```@Test
public void connect05() throws IOException, ClassNotFoundException, SQLException {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream('src\\mysql.properties'));
//获取相关的值
String user = properties.getProperty('user');
String password = properties.getProperty('password');
String driver = properties.getProperty('driver');
String url = properties.getProperty('url');
Class.forName('com.mysql.cj.jdbc.Driver');//建议写上 不写也没事,因为导入的驱动里的META-INF里的services里的文件
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println('方式5'+connection);
赞0
踩0