package com.achievo.ems.web.service.score;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnUtils2 {
public static Logger logger = LoggerFactory.getLogger(ConnUtils2.class);
private final static String DRIVER_1 = "oracle.jdbc.driver.OracleDriver";
private final static String URL_1 = "jdbc:oracle:thin:@10.5.117.28:1521:orcl";
private final static String USERNAME_1 = "emis";
private final static String PASSWORD_1 = "emis";
private final static String DRIVER_2 = "oracle.jdbc.driver.OracleDriver";
private final static String URL_2 = "jdbc:oracle:thin:@10.5.119.132:1521:kaowu";
private final static String USERNAME_2 = "ems";
private final static String PASSWORD_2 = "emis2013";
public static Connection getConnect_1() {
Connection conn = null;
try {
Class.forName(DRIVER_1).newInstance();
conn = DriverManager.getConnection(URL_1, USERNAME_1, PASSWORD_1);
} catch (Exception e) {
throw new RuntimeException("获取连接失败", e);
}
return conn;
}
public static Connection getConnect_2() {
Connection conn = null;
try {
Class.forName(DRIVER_2).newInstance();
conn = DriverManager.getConnection(URL_2, USERNAME_2, PASSWORD_2);
} catch (Exception e) {
throw new RuntimeException("获取连接失败", e);
}
return conn;
}
public static void main(String[] args) throws Throwable {
ConnUtils2 connUtils = new ConnUtils2();
connUtils.testEncrypt(1);
// AtomicInteger ai = new AtomicInteger(10);
//
// while (ai.get() > 0) {
// int intValue = ai.getAndDecrement();
// System.out.println(intValue);
// }
}
/**
* 加签测试
*
* @param whichDb
* @throws Throwable
*/
public void testEncrypt(int whichDb) throws Throwable {
Connection conn = null;
if (1 == whichDb) {
conn = ConnUtils2.getConnect_1();
} else {
conn = ConnUtils2.getConnect_2();
}
String sql = " select SCORE_ID as scoreId "//
+ " ,ID as csubjId "//
+ " ,EXAMINEE_ID as creditId "//
+ " ,SUBJECTIVE_RESULT as score "//
+ " ,FLAG as flag "//
+ " ,exception as exceptionFlag "//
+ " ,SIGNATURE as signature "//
+ " ,CREATE_TIME as createTime "//
+ " ,UPDATE_TIME as updateTime "//
+ " ,CREATE_BY as createBy "//
+ " ,UPDATE_BY as updateBy "//
+ " ,APPLY_NO as applyNo "//
+ " from P_SUBJECTIVE_SCORE_ORIG_VERNON "//
;
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
List<String> columnNameList = new ArrayList<String>();
for (int i = 1; i <= columnCount; i++) {
String columnName = md.getColumnName(i);
columnNameList.add(columnName);
}
List<PSubjectiveScoreOrigVernon> pssoList = new ArrayList<PSubjectiveScoreOrigVernon>();
while (rs.next()) {
PSubjectiveScoreOrigVernon psso = new PSubjectiveScoreOrigVernon();
for (String columnLabel : columnNameList) {
String value = rs.getString(columnLabel);
BeanUtils.setProperty(psso, columnLabel, value);
}
pssoList.add(psso);
}
try {
EncryptDecryptTask4Test.getInstance().encrypt(pssoList);
if (logger.isDebugEnabled()) {
logger.debug("加签成功");
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("加签失败");
}
}
for (int i = 0; i < 10; i++) {
if (logger.isDebugEnabled()) {
PSubjectiveScoreOrigVernon psso = pssoList.get(i);
logger.debug(psso.getEncryptedInfo());
}
}
for (int i = pssoList.size() - 10; i < pssoList.size(); i++) {
if (logger.isDebugEnabled()) {
PSubjectiveScoreOrigVernon psso = pssoList.get(i);
logger.debug(psso.getEncryptedInfo());
}
}
try {
EncryptDecryptTask4Test.getInstance().decrypt(pssoList);
if (logger.isDebugEnabled()) {
logger.debug("验签成功");
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("验签失败");
}
}
}
@SuppressWarnings("unused")
private void encryptByMultiThreads(List<PSubjectiveScoreOrigVernon> pssoList) {
long begin_4_multi_threads = System.currentTimeMillis();
System.out.println("多线程加签---开始=" + begin_4_multi_threads);
EncryptDecryptTask4Test.getInstance().encrypt(pssoList);
long end_4_multi_threads = System.currentTimeMillis();
System.out.println("多线程加签---结束=" + end_4_multi_threads);
long ms_4_multi_threads = (end_4_multi_threads - begin_4_multi_threads);
System.out.println("多线程加签花费毫秒数=" + ms_4_multi_threads);
String ms2Hms_4_multi_threads = ConnUtils2.ms2Hms(ms_4_multi_threads);
System.out.println("多线程加签共耗时共耗时:" + ms2Hms_4_multi_threads);
}
@SuppressWarnings("unused")
private void encryptBySingleThreads(List<PSubjectiveScoreOrigVernon> pssoList) {
long begin_4_single_threads = System.currentTimeMillis();
System.out.println("单线程加签---开始=" + begin_4_single_threads);
EncryptDecryptUtils4Test5.encrypt(pssoList);
long end_4_single_threads = System.currentTimeMillis();
System.out.println("单线程加签---结束=" + end_4_single_threads);
long ms_4_single_threads = (end_4_single_threads - begin_4_single_threads);
System.out.println("单线程加签花费毫秒数=" + ms_4_single_threads);
String ms2Hms_4_single_threads = ConnUtils2.ms2Hms(ms_4_single_threads);
System.out.println("单线程加签共耗时:" + ms2Hms_4_single_threads);
}
// oracle.sql.Clob类型转换成String类型
public static String clobToString(Clob clob) {
String reString = "";
Reader is = null;
try {
is = clob.getCharacterStream();
} catch (SQLException e) {
e.printStackTrace();
}
// 得到流
BufferedReader br = new BufferedReader(is);
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
while (s != null) {
// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
reString = sb.toString();
return reString;
}
public static String ms2Hms(long ms) {
// 毫秒 秒 分 时
long hour = ms / 1000 / 60 / 60;
// 毫秒 秒 分
long min = ms / 1000 / 60 % 60;
// 毫秒 秒
long sec = ms / 1000 % 60;
// 毫秒 秒
long mi = ms % 1000;
java.lang.String x = hour + "时" + min + "分" + sec + "秒" + mi + "毫秒";
// java.lang.String x = hour + "H" + min + "M" + sec + "S" + mi + "MS";
return x;
}
}
ConnUtils2程序耗时输出 oracle.sql.Clob类型转换成String类型
猜你喜欢
转载自vernonchen163.iteye.com/blog/2037852
今日推荐
周排行