MinerDB.java 数据库工具类

MinerDB.java 数据库工具类

package com.iteye.injavawetrust.miner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; /**
* 数据库工具类
* @author InJavaWeTrust
*
*/
public class MinerDB { private static final Log LOG = LogFactory.getLog(MinerDB.class); private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null; private MinerDB() { } private static final MinerDB instance = new MinerDB(); public static MinerDB getInstance() {
return instance;
} /**
* 连接数据库
*
* @return
*/
public Connection connection() {
try {
Class.forName(MinerConstanits.DRIVER);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
conn = DriverManager.getConnection(MinerConstanits.DBURL,
MinerConstanits.USER, MinerConstanits.PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} /**
* 关闭连接
*
* @param rs
* @param st
* @param conn
*/
public void release(ResultSet rs, Statement st, Connection conn) {
try {
try {
if (null != rs) {
rs.close();
}
} catch (Exception e) {
rs = null;
}
try {
if (null != st) {
st.close();
}
} catch (Exception e) {
st = null;
}
try {
if (null != conn) {
conn.close();
}
} catch (Exception e) {
conn = null;
}
} finally {
rs = null;
st = null;
conn = null;
}
} /**
* 得到UUID
* @return 32位UUID
*/
public String getUUID() {
String s = UUID.randomUUID().toString();
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
+ s.substring(19, 23) + s.substring(24);
} /**
* insert sql
* @param bean MinerBean obj
* @return insert sql
*/
public synchronized String getInsertSql(MinerBean bean) {
return "insert into miner (id, batchNo, filePath, fileName, getDate) values ('"
+ bean.getId()
+ "','"
+ bean.getBatchNo()
+ "','"
+ bean.getFilePath()
+ "','"
+ bean.getFileName()
+ "',NOW())";
} /**
* 插入
* @param sql
*/
public synchronized void insert(String sql){
try{
MinerDB.getInstance().connection();
st = conn.createStatement();
st.execute(sql);
MinerDB.getInstance().release(rs, st, conn);
}catch(Exception e){
LOG.info("database error[" + sql + "]");
LOG.info("insert 异常信息[" + e.getMessage() + "]");
}
} }

返回列表

上一篇:Java json工具类,jackson工具类,ObjectMapper工具类


下一篇:Java日期工具类,Java时间工具类,Java时间格式化