- 加载驱动
- 创建连接(url,用户名,密码)
- prepareStatement预编译
- 执行sql
一、最简单的方法(方法)
@Test
public void test() {
Connection connect = null;
try{
// 1、通过反射获取驱动实例
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver)clazz.newInstance();
String url = "jdbc:mysql://localhost:3306/funddb?useSSL=false&serverTimezone=UTC";
Properties properties = new Properties();
properties.setProperty("user", "leo-zu");
properties.setProperty("password", "123456");
// 2、获取数据库连接
connect = driver.connect(url, properties);
System.out.println(connect);
// 3、使用prepareStatement,具有预编译,防sql注入等功能
String sql = "select * from fund_role";
PreparedStatement prepareStatement = connect.prepareStatement(sql);
ResultSet resultSet = prepareStatement.executeQuery();
if (resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("role_name"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (connect != null){
try {
connect.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
二、第二种方法
使用DriverManager获取连接,通过发射加载driver的静态代码块,会自动创建驱动并注册
@Test
public void test2() {
Connection connect = null;
try{
// 1、通过反射加载驱动时,driver静态代码块会注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/funddb?useSSL=false&serverTimezone=UTC";
Properties properties = new Properties();
properties.setProperty("user", "leo-zu");
properties.setProperty("password", "123456");
// 2、使用DriverManager获取数据库连接
connect = DriverManager.getConnection(url, properties);
// 或者直接connect = DriverManager.getConnection(url, "leo-zu", "123456");
// 3、使用prepareStatement,具有预编译,防sql注入等功能
String sql = "select * from fund_role";
PreparedStatement prepareStatement = connect.prepareStatement(sql);
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("role_name"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (connect != null){
try {
connect.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
三、使用配置文件
@Test
public void test3() {
Connection connect = null;
try{
// 1、加载配置文件
InputStream inputStream = JdbcTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(inputStream);
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driverClass = properties.getProperty("driverClass");
// 2、通过反射加载驱动时,driver静态代码块会注册驱动
Class.forName(driverClass);
// 3、使用DriverManager获取数据库连接
connect = DriverManager.getConnection(url, user, password);
// 4、使用prepareStatement,具有预编译,防sql注入等功能
String sql = "select * from fund_role";
PreparedStatement prepareStatement = connect.prepareStatement(sql);
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("role_name"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (connect != null){
try {
connect.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
user=用户名
password=密码
url=jdbc:mysql://localhost:3306/数据库?useSSL=false&serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver