日期:2014-05-16 浏览次数:20639 次
package com.core.util;import java.sql.connection;import java.sql.date;import java.sql.drivermanager;import java.sql.resultset;import java.sql.resultsetmetadata;import java.sql.sqlexception;import java.sql.statement;import java.sql.timestamp;import java.util.arraylist;import java.util.linkedhashmap;import java.util.list;import org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;import com.core.exception.serviceexception;public final class dbutil { private static final log log = logfactory.getlog(dbutil.class); // 定义一个数据库连接对象 private dbutil() {} private static final threadlocal<connection> connection = new threadlocal<connection>(); public static connection getconnection() { connection conn = connection.get(); if(conn == null){ try { class.forname("org.logicalcobwebs.proxool.proxooldriver"); conn = drivermanager.getconnection("proxool.db"); connection.set(conn); } catch (classnotfoundexception e) { log.error("#error# :创建数据库连接发生异常,请检查!", e); }catch (sqlexception e) { log.error("#error# :创建数据库连接发生异常,请检查!", e); } } return conn; } /** * 执行数据库增、删、改操作. * * @param sql * 待操作的sql字符串 * @return 执行结果所影响的行数,异常则返回-1 * @throws sqlexception * @throws exception */ public static int executeupdate(string sql){ int exeok = -1; statement stmt = null; try { stmt = getconnection().createstatement(); exeok = stmt.executeupdate(sql); } catch (sqlexception e) { log.error("#error# :执行sql语句出错,请检查!\n" + sql, e); }finally{ close(stmt); } return exeok; } /** * 执行批处理操作一般用于事务 * * @param vsqlstr 传入的被操作sql字符串数组 * @return 执行批处理后所影响的行数 * @throws exception */ @suppresswarnings("unchecked") public static int[] executeupdate(list sqllist){ int updatecounts[] = null; statement stmt = null; connection conn = getconnection(); try { stmt = conn.createstatement(); conn.setautocommit(false); for (int i = 0; i < sqllist.size(); i++) { string sql = sqllist.get(i) + ""; stmt.addbatch(sql); } updatecounts = stmt.executebatch(); conn.commit(); conn.setautocommit(true); } catch (sqlexception e) { try { conn.rollback(); } catch (sqlexception e1) { log.error("#error# :执行数据回滚出错,请检查!", e); } log.error("#error# :执行批量sql语句出错,请检查!", e); }finally{ close(stmt); } return updatecounts; } @suppresswarnings("unchecked") public static rslist executequery(string query){ resultset resultset = null; resultsetmetadata metadata = null; rslist rs = null; statement stmt = null; try { stmt = getconnection().createstatement(); resultset = stmt.executequery(query); int colnumber = 0; linkedhashmap<string, string> recomap; string colname = ""; string colvalue = ""; rs = new rslist(); while(resultset != null && resultset.next()){ recomap = new linkedhashmap<string, string>(); metadata = resultset.getmetadata(); colnumber = metadata.getcolumncount(); for (int i = 1; i <colnumber+1;i++) { colname = metadata.getcolumnlabel(i) .tolowercase(); colvalue = resultset.getstring(i); if(!stringutil.isblank(colvalue)){ recomap.put(colname, colvalue); }else{ recomap.put(colname, ""); } } rs.add(recomap); } } catch (sqlexception e) { log.error("#error# :执行sql语句出错,请检查!\n" + query, e); } finally { close(stmt,resultset); } return rs; } /** * 取得数据库服务器的当前日期(年-月-日 时:分:秒)。 * @return 数据库服务器的当前日期,java.sql.timestamp对象。 */ public static timestamp getcurrenttime(){ string sql = "select sysdate as datevalue from dual"; resultset rs = null; timestamp dtrtn = null; statement stmt = null; try { stmt = getconnection().createstatement(); rs = stmt.executequery(sql); while (rs != null && rs.next()) { dtrtn = rs.gettimestamp(1); } } catch (sqlexception e) { log.error("#error# :执行sql语句出错,请检查!\n"