需要完成的内容
1. 数据库连接对象java.sql.Connection获取过程
2. 关闭资源
JDBC工具类
1. 所有的方法都是static修饰的静态方法
2. 需要考虑自动加载过程,完成一些必要数据的自动处理
url
driver
user
password
3. 所需数据库连接条件保存到文件中
4. 关闭方法提供多种多样组合方法
【注意】
db.properties文件保存到src目录下
# 当前JDBC连接所需的驱动
driverClass=com.mysql.jdbc.Driver
# 数据库连接符合JDBC规范的url
url=jdbc:mysql://localhost:3306/nzgp2001?useSSL=true
# 用户名
user=root
# 密码
password=123456
package util;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JdbcUtil {
private static String url = null;
private static String user = null;
private static String password = null;
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("./src/db.properties"));
String driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driverClass);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection) {
close(connection, null, null);
}
public static void close(Connection connection, Statement statement) {
close(connection, statement, null);
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2 PreparedStatement使用
2.1 PreparedStatement插入数据SQL完成
@Test
public void testInsert() {
User user = new User(10, "逗比匿名君", "123456");
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtil.getConnection();
String sql = "insert into nzgp2001.user(id, userName, password) VALUE (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, user.getId());
preparedStatement.setObject(2, user.getUserName());
preparedStatement.setObject(3, user.getPassword());
int affectedRows = preparedStatement.executeUpdate();
System.out.println("affectedRows:" + affectedRows);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement);
}
}
2.2 PreparedStatment修改SQL完成
@Test
public void testUpdate() {
User user = new User(10, "逗比匿名君", "航海中路彭于晏");
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtil.getConnection();
String sql = "update user set userName = ?, password = ? where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, user.getUserName());
preparedStatement.setObject(2, user.getPassword());
preparedStatement.setObject(3, user.getId());
int affectedRows = preparedStatement.executeUpdate();
System.out.println("affectedRows:" + affectedRows);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement);
}
}
##### 3.3 PreparedStatment删除SQL完成
```java
@Test
public void testDelete() {
int id = 7;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtil.getConnection();
String sql = "delete from user where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, id);
int affectedRows = preparedStatement.executeUpdate();
System.out.println("affectedRows:" + affectedRows);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement);
}
}
2.4 PreparedStatment查询SQL完成
@Test
public void testSelectOne() {
int id = 10;
User user = null;
ResultSet resultSet = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtil.getConnection();
String sql = "select * from user where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, id);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String userName = resultSet.getString("userName");
String password = resultSet.getString("password");
user = new User(id, userName, password);
}
if (user != null) {
System.out.println(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement, resultSet);
}
}
@Test
public void testSelectAll() {
List<User> list = new ArrayList<>();
ResultSet resultSet = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtil.getConnection();
String sql = "select * from user";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String userName = resultSet.getString("userName");
String password = resultSet.getString("password");
list.add(new User(id, userName, password));
}
for (User user : list) {
System.out.println(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement, resultSet);
}
}