2011-07-26
jsp连接数据库sqlserve
给你一个我很久以前写的,现在都已经不在使用这低级的用法了。建议你学学spring和hibernate,这个用起来更方便,实用。/** DataBaseConn。java** Created on 2006年1月26日, 上午10:19* 连接管理*/package ccb。 pub;import java。sql。*;import javax。sql。*;import ming。*;/**** @author*/public final class DataBaseConn {private static int iUsed = 0;/** Creat...全部
给你一个我很久以前写的,现在都已经不在使用这低级的用法了。建议你学学spring和hibernate,这个用起来更方便,实用。/* * DataBaseConn。java * * Created on 2006年1月26日, 上午10:19 * 连接管理 */package ccb。
pub;import java。sql。*;import javax。sql。*;import ming。*;/** * * @author */public final class DataBaseConn { private static int iUsed = 0; /** Creates a new instance of DataBaseConn */ private DataBaseConn() { } /* * 向数据库连接池申请一个数据连接 */ public static Connection getConn(){ Context ctx = null; DataSource ds = null; Connection cnn=null; String envString = tLinkPool(); try { ctx=new InitialContext(); if(ctx==null) throw new Exception("没有匹配的环境"); ds=(DataSource)ctx。
lookup(envString); if(ds==null) throw new Exception("没有匹配数据库"); cnn= tConnection(); //设置连接的事务提交方式均为自动提交 tAutoCommit(true); Debug。
logWithDetailByLevel("建立连接成功", 0, 2); iUsed++; StringBuffer msg = new StringBuffer(""); msg。
append("连接占用:"); msg。append(iUsed); Debug。logWithDetailByLevel( String(),0,2); }catch(Exception e) { Debug。
log(e); } return cnn; } /* * 将已经使用完毕的数据库连接返还给连接池 * 参数:conn 要返还的数据库连接 */ public static void closeConn(Connection conn){ if (conn != null){ try { ose(); Debug。
logWithDetailByLevel("断开连接成功", 0, 2); iUsed--; StringBuffer msg = new StringBuffer(""); msg。
append("连接占用:"); msg。append(iUsed); Debug。logWithDetailByLevel( String(),0,2); }catch(SQLException sqle){ Debug。
log(sqle); }finally{ conn = null; } } } }/* * DataBase。
java