67,512
社区成员
发帖
与我相关
我的任务
分享
public class TConnection {
private static Connection conn;
public static Connection getConnection(){
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 创建连接
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3:3306/data","root","root");
}catch (Exception e) {
conn=null;
MessageDao md = new MessageDao();
//发送异常消息,通知前台
if(! md.dbExceptionMessageAgain())
md.sendMessage(null, 42);
md = null;
}
//返回连接
return conn;
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn) {
try {
if(rs!=null) rs.close();
if(pst!=null) pst.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
rs = null;
pst = null;
conn = null;
}
}
public static void closeConnection(Connection conn) {
try {
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn = null;
}
}
}
//应用
public void ways1(){
Connection connT=null;
connT = TConnection.getConnection();
if(connT==null){
request.setAttribute("error", "服务器繁忙,请稍后再试.");
ConnFactory.close(connP);
return;
}
...
TConnection.close(connT);
}