aggregation-platform/src/com/platform/oracle/OracleConnector.java

197 lines
5.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.platform.oracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import com.base.Custom4exception;
import com.base.CustomException;
import com.platform.entities.OracleConnectorParams;
import com.platform.utils.Configs;
import com.platform.utils.FileOperateHelper;
public class OracleConnector {
public static Logger log = Configs.DAILY_ROLLING_LOGGER
.getLogger(OracleConnector.class);
public OracleConnector() {
}
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Configs.CONSOLE_LOGGER.info("Oracle驱动加载成功");
} catch (ClassNotFoundException e) {
log.error(Custom4exception.OracleSQL_Except, e);
}
}
public synchronized static Connection connectionBuilder(String url,
String user, String password, OracleConnectorParams oc)
throws CustomException {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
Configs.CONSOLE_LOGGER.info("创建oracle连接失败: [" + e.getMessage()
+ "]");
if (null != oc) {
FileOperateHelper.fileWrite(
Configs.EXTRACT_LOG_LOCALTION + oc.getName() + ".log",
"创建oracle连接失败: [" + e.getMessage() + "]\r\n");
}
throw new CustomException(Custom4exception.OracleSQL_Except, e);
}
return conn;
}
public synchronized static boolean canConnect(String url, String user,
String password) {
Connection result = null;
try {
result = connectionBuilder(url, user, password, null);
} catch (CustomException e) {
log.error(Custom4exception.OracleSQL_Except, e);
}
if(null != result){
try {
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
else{
return false;
}
}
/** 不要使用这个方法 statement没法关闭
* @param conn
* @param sql
* @param filePath
* @return
*/
public synchronized static ResultSet getSQLExecResultSet(Connection conn,
String sql, String filePath) {
ResultSet resultSet = null;
if (null != filePath) {
filePath = filePath.replace(".log", "");
}
Statement statement = null;
try {
FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n");
statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
resultSet = statement.executeQuery(sql);
/*
* if(resultSet.next()){ System.out.println(resultSet.getInt(1)); }
*/
FileOperateHelper.fileWrite(filePath + ".log", "OK \r\n");
} catch (SQLException e) {
FileOperateHelper.fileWrite(filePath + ".log",
e.getMessage() + "\r\n");
log.error(Custom4exception.OracleSQL_Except, e);
} finally {
if (null != statement) {
try {
statement.close();
} catch (SQLException e) {
log.error(Custom4exception.OracleSQL_Except, e);
}
}
}
return resultSet;
}
/** 不要使用这个方法 statement没法关闭
* @param url
* @param user
* @param password
* @param sql
* @param filePath
* @return
*/
public synchronized static ResultSet getSQLExecResultSet(String url,
String user, String password, String sql, String filePath) {
ResultSet result = null;
try {
result = getSQLExecResultSet(
connectionBuilder(url, user, password, null), sql, filePath);
} catch (CustomException e) {
log.error(Custom4exception.OracleSQL_Except, e);
}
return result;
}
/**
* 执行对oracle数据库的增、删
*
* @param conn
* @param sql
* @return true:执行的不返回集合数据的sql成功 是否执行成功
*/
public synchronized static boolean execOracleSQL(Connection conn,
String sql, String filePath) {
if (null != filePath) {
filePath = filePath.replace(".log", "");
}
boolean flag = false;
Statement statement = null;
try {
FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n");
statement = conn.createStatement();
int res = statement.executeUpdate(sql);
flag = true;
FileOperateHelper.fileWrite(filePath + ".log", "执行结果:" + res
+ " \r\n");
statement.close();
} catch (SQLException e) {
flag = false;
FileOperateHelper.fileWrite(filePath + ".log",
e.getMessage() + "\r\n");
log.error(Custom4exception.OracleSQL_Except, e);
}
return flag ;
}
/**
* 执行对oracle数据库的返回集合数据的sql
*
* @param conn
* @param sql
* @return true:执行结果大于1即有数据 是否执行成功
*/
public synchronized static boolean execUpdateOracleSQL(Connection conn,
String sql, String filePath) {
if (null != filePath) {
filePath = filePath.replace(".log", "");
}
boolean flag = false;
Statement statement = null;
try {
FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n");
statement = conn.createStatement();
int res = statement.executeUpdate(sql);
flag = true;
FileOperateHelper.fileWrite(filePath + ".log", "执行结果:"+ res
+ " \r\n");
statement.close();
} catch (SQLException e) {
flag = false;
FileOperateHelper.fileWrite(filePath + ".log",
e.getMessage() + "\r\n");
log.error(Custom4exception.OracleSQL_Except, e);
}
return flag;
}
}