目录
背景
一、获取数据库连接
二、关闭数据库连接
三、获取查询数据
四、对数据进行加密并批量更新
今天需要实现将Oracle数据库中的指定字段取出来,使用Base64进行加密,然后将加密结果存回去,涉及到Oracle数据库的操作。
public static Connection getConnection(){
Connection conn = null;
//设置数据库的连接地址
// String url = "jdbc:oracle:thin:@localhost:1521:test";//改格式数据库连接时一直报错
String url = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT =1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME =test)))";
String user = "crawlm";
String passwd = "crawlm_Sd";
try{
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//获取数据库连接
conn = DriverManager.getConnection(url,user,passwd);
} catch (ClassNotFoundException e){
e.printStackTrace();
System.out.println("加载数据库启动失败!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("获取数据库连接失败!");
}
return conn;
}
public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs){
//关闭结果集对象
try{
if (rs != null){
rs.close();
}
}catch (SQLException e){
e.printStackTrace();
System.out.println("关闭ResultSet失败!");
}
//关闭SQL语句预编译对象
try {
if (ps != null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("关闭PreparedStatement失败!");
}
//关闭数据库连接
try{
if (conn != null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
System.out.println("关闭Connetion失败!");
}
}
public static ResultSet getResultSet(){
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
String querySql = "select KEYWORDS1 from KEYWORDS_ALL";
try{
conn = OracleUtil.getConnection();
if (conn == null){
System.out.println("获取连接失败");
}
statement = conn.prepareStatement(querySql);
rs = statement.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
return rs;
}finally {
OracleUtil.closeConnection(conn,statement,rs);
return rs;
}
}
public static int updateBatchData(List data){
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
//更新SQL语句
String updateSql = "update KEYWORDS_ALL set KEYWORDS2=? where KEYWORDS1=?";
try{
conn = OracleUtil.getConnection();
if (conn == null){
System.out.println("获取连接失败");
}
//关闭自动提交事务
conn.setAutoCommit(false);
//创建一个 PreparedStatement 对象来将参数化的 SQL语句发送到数据库
statement = conn.prepareStatement(updateSql);
//将一组参数添加到此 PreparedStatement 对象的批处理命令中
for (KeyWords2Base64 subdata: data){
statement.setString(1,subdata.getKeyword2());
statement.setString(2,subdata.getKeyword1());
statement.addBatch();
}
//执行更新操作
statement.executeBatch();
//语句执行完毕,提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(1);
return -1;
}finally {
OracleUtil.closeConnection(conn,statement,rs);
System.out.println(2);
return -2;
}
}
五、完整代码
package program;
import java.sql.*;
import java.util.List;
/**
* @ClassName OracleUtil
* @Date 2020/3/26 8:34
* @Description
* Oracle数据库工具类
*/
public class OracleUtil {
/**
* 获取Oracle数据库连接
* @return connnetion
*/
public static Connection getConnection(){
Connection conn = null;
//设置数据库的连接地址
// String url = "jdbc:oracle:thin:@localhost:1521:test";//改格式数据库连接时一直报错
String url = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT =1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME =test)))";
String user = "crawlm";
String passwd = "crawlm_Sd";
try{
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//获取数据库连接
conn = DriverManager.getConnection(url,user,passwd);
} catch (ClassNotFoundException e){
e.printStackTrace();
System.out.println("加载数据库启动失败!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("获取数据库连接失败!");
}
return conn;
}
/**
* 关闭数据库连接方法
* @param conn Connection
* @param ps PreparedStatement
* @param rs ResultSet
*/
public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs){
//关闭结果集对象
try{
if (rs != null){
rs.close();
}
}catch (SQLException e){
e.printStackTrace();
System.out.println("关闭ResultSet失败!");
}
//关闭SQL语句预编译对象
try {
if (ps != null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("关闭PreparedStatement失败!");
}
//关闭数据库连接
try{
if (conn != null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
System.out.println("关闭Connetion失败!");
}
}
/**
* 获取数据库数据方法
* @return
*/
public static ResultSet getResultSet(){
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
String querySql = "select KEYWORDS1 from KEYWORDS_ALL";
try{
conn = OracleUtil.getConnection();
if (conn == null){
System.out.println("获取连接失败");
}
statement = conn.prepareStatement(querySql);
rs = statement.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
return rs;
}finally {
OracleUtil.closeConnection(conn,statement,rs);
return rs;
}
}
/**
* 更新数据库方法
* @param data
* @return
*/
public static int updateBatchData(List data){
Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
//更新SQL语句
String updateSql = "update KEYWORDS_ALL set KEYWORDS2=? where KEYWORDS1=?";
try{
conn = OracleUtil.getConnection();
if (conn == null){
System.out.println("获取连接失败");
}
//关闭自动提交事务
conn.setAutoCommit(false);
//创建一个 PreparedStatement 对象来将参数化的 SQL语句发送到数据库
statement = conn.prepareStatement(updateSql);
//将一组参数添加到此 PreparedStatement 对象的批处理命令中
for (KeyWords2Base64 subdata: data){
statement.setString(1,subdata.getKeyword2());
statement.setString(2,subdata.getKeyword1());
statement.addBatch();
}
//执行更新操作
statement.executeBatch();
//语句执行完毕,提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(1);
return -1;
}finally {
OracleUtil.closeConnection(conn,statement,rs);
System.out.println(2);
return -2;
}
}
}