package cn.cslg.xk.util;
/**
* @author 李庆
*@Email Kingstarforever@gmail.com
*@dtaeTime 2010-4-2
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class ConnectionManger {
private static ThreadLocal<Connection> connectionHoder = new ThreadLocal<Connection>();
public static Connection getConnection() {
Connection conn = connectionHoder.get();
if (conn == null) {
try {
conn = JndiBean.getConnection();
connectionHoder.set(conn);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
//throw new AppException("系统出现故障,请联系管理员!");
}
}
return conn;
}
public static void closeConnection() {
Connection conn = connectionHoder.get();
if (conn != null) {
try {
conn.close();
connectionHoder.remove();
} catch (SQLException e) {
}
}
}
public static void rollback() {
try {
Connection conn = connectionHoder.get();
if (conn != null) {
conn.rollback();
}
} catch (SQLException e) {
}
}
public static void setCommit(boolean b) {
try {
Connection conn = connectionHoder.get();
if (conn != null) {
conn.setAutoCommit(b);
}
} catch (SQLException e) {
}
}
public static void Commit() {
try {
Connection conn = connectionHoder.get();
if (conn != null) {
conn.commit();
}
} catch (SQLException e) {
}
}
public static void closePs(PreparedStatement ps) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
}
}
public static void closeRs(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}
public static void closeStatment(Statement s) {
try {
if (s != null) {
s.close();
}
} catch (SQLException e) {
}
}
}
没有评论:
发表评论