package com.hspedu.jdbc;
import com.mysql.jdbc.Driver;
import org.junit.jupiter.api.Test;
import java.beans.Transient;
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.sql.Statement;
import java.util.Properties;
/**
* @author: guorui fu
* @versiion: 1.0
* 分析java连接MySQL的五种方式
*/
public class jdbcConn {
@Test
public void connect01() throws SQLException {
//1.注册驱动
Driver driver = new Driver();
//2.得到连接
String url = "jdbc:mysql://localhost:3306/hsp_db02";
// 将用户名和密码封装到properties对象 user与password 是规定好的key,值自己写
Properties properties = new Properties();
properties.setProperty("user","root");//用户
properties.setProperty("password","fgr");//密码
// connect 是连接
Connection connect = driver.connect(url, properties);
//3.执行sql
String sql = "delete from actor where id = 1";
//用于执行静态sql语句并返回生成结果的对象
Statement statement = connect.createStatement();
int row = statement.executeUpdate(sql);//如果是dml语句,返回的是影响的行数
System.out.println(row > 0 ? "成功":"失败");
//4.关闭连接资源
statement.close();
connect.close();
}
//方式2
@Test
public void connect02() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
//1.反射加载Driver类,动态加载,减少了依赖性
Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)cls.newInstance();
//2.得到连接
String url = "jdbc:mysql://localhost:3306/hsp_db02";
// 将用户名和密码封装到properties对象 user与password 是规定好的key,值自己写
Properties properties = new Properties();
properties.setProperty("user","root");//用户
properties.setProperty("password","fgr");//密码
Connection connect = driver.connect(url, properties);
System.out.println("方式2" + connect);
}
//方式3 使用Drivermanager 替代 Driver 进行统一管理
@Test
public void connect03() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)cls.newInstance();
//创建url 和user ,password
String url = "jdbc:mysql://localhost:3306/hsp_db02";
String user = "root";//用户
String password = "fgr";//密码
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 类是,完成注册,底层注册Driver在静态代码块中进行
Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
//创建url 和user ,password
String url = "jdbc:mysql://localhost:3306/hsp_db02";
String user = "root";//用户
String password = "fgr";//密码
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("第四种方式" + connection);
}
//[推荐,简洁]方式5 在方式4基础上改进,使用配置文件,连接数据库更加灵活
@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(driver);//建议写
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式5" + connection);
Statement statement = connection.createStatement();
String sql = "insert into ";
int i = statement.executeUpdate(sql);
System.out.println(i > 0 ? "成功" : "失败");
statement.close();
connection.close();
}
}
08-21
08-14
293
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-15
524
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-04
1469
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)