1、创建连接
/**
* 获取数据库连接
* 加载数据库驱动并建立连接
*/
public static void getConnection() {
try {
// 加载驱动程序
Class.forName("org.postgresql.Driver");
// 建立连接
String url = "jdbc:postgresql://localhost:5432/dbName";
String username = "postgres";
String password = "possword";
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// 异常处理
e.printStackTrace();
}
}
2、操作数据库
从数据库中读取用户信息,并加密电话号码后更新到数据库
/**
* 主函数入口
* 用于从数据库中读取用户信息,并加密电话号码后更新到数据库
* @param args 命令行参数
*/
public static void main(String[] args) {
// 存储待执行的SQL更新语句
List<String> sqls = new ArrayList<>();
// 获取数据库连接
getConnection();
try {
// 创建声明对象,用于执行SQL语句
Statement statement = connection.createStatement();
// 执行查询语句,获取结果集
ResultSet resultSet = statement.executeQuery("select * from sys_user");
// 设置手动提交事务,以提高性能
connection.setAutoCommit(false);
// 遍历结果集
while (resultSet.next()) {
// 获取用户ID和电话号码
Long id = resultSet.getLong("user_id");
String phonenumber = resultSet.getString("phonenumber");
// 检查电话号码长度是否为11位
if (phonenumber.length() == 11) {
// 加密电话号码
String encryptedPhonenumber = encrypt(phonenumber, "sxsgdzbphjgpt@24");
// 输出更新语句
System.out.println("sql:"+"update sys_user set phonenumber='" + encryptedPhonenumber + "' where user_id=" + id);
// 将更新语句添加到列表中
sqls.add("update sys_user set phonenumber='" + encryptedPhonenumber + "' where user_id=" + id);
}
}
// 关闭结果集
resultSet.close();
// 执行更新操作
for (String sql : sqls) {
statement.executeUpdate(sql);
}
// 提交事务
connection.commit();
}catch (Exception e){
// 异常处理
e.printStackTrace();
}
// 关闭数据库连接
closeConnection();
}
3、关闭连接
/**
* 关闭数据库连接
* 释放数据库资源
*/
public static void closeConnection() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// 异常处理
e.printStackTrace();
}
}
4、完整案例
package com.kang.jdbc;
import com.kang.sm4.EncryptUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 修改密码工具类
* 用于加密数据库中的电话号码,以增强数据安全性
*/
public class ChangePasswd {
// 数据库连接对象
private static Connection connection = null;
/**
* 主函数入口
* 用于从数据库中读取用户信息,并加密电话号码后更新到数据库
* @param args 命令行参数
*/
public static void main(String[] args) {
// 存储待执行的SQL更新语句
List<String> sqls = new ArrayList<>();
// 获取数据库连接
getConnection();
try {
// 创建声明对象,用于执行SQL语句
Statement statement = connection.createStatement();
// 执行查询语句,获取结果集
ResultSet resultSet = statement.executeQuery("select * from sys_user");
// 设置手动提交事务,以提高性能
connection.setAutoCommit(false);
// 遍历结果集
while (resultSet.next()) {
// 获取用户ID和电话号码
Long id = resultSet.getLong("user_id");
String phonenumber = resultSet.getString("phonenumber");
// 检查电话号码长度是否为11位
if (phonenumber.length() == 11) {
// 加密电话号码
String encryptedPhonenumber = encrypt(phonenumber, "sxsgdzbphjgpt@24");
// 输出更新语句
System.out.println("sql:"+"update sys_user set phonenumber='" + encryptedPhonenumber + "' where user_id=" + id);
// 将更新语句添加到列表中
sqls.add("update sys_user set phonenumber='" + encryptedPhonenumber + "' where user_id=" + id);
}
}
// 关闭结果集
resultSet.close();
// 执行更新操作
for (String sql : sqls) {
statement.executeUpdate(sql);
}
// 提交事务
connection.commit();
}catch (Exception e){
// 异常处理
e.printStackTrace();
}
// 关闭数据库连接
closeConnection();
}
/**
* 加密方法
* 使用SM4算法对密码进行加密
* @param passwd 明文密码
* @param key 加密密钥
* @return 加密后的密码
*/
public static String encrypt(String passwd, String key) {
return EncryptUtils.encryptBySm4(passwd, key);
}
/**
* 获取数据库连接
* 加载数据库驱动并建立连接
*/
public static void getConnection() {
try {
// 加载驱动程序
Class.forName("org.postgresql.Driver");
// 建立连接
String url = "jdbc:postgresql://localhost:5432/dbName";
String username = "postgres";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// 异常处理
e.printStackTrace();
}
}
/**
* 关闭数据库连接
* 释放数据库资源
*/
public static void closeConnection() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// 异常处理
e.printStackTrace();
}
}
}
评论区