大家看看我的项目连接数据库有没有毛病。我们的连接池总挂。项目不好使了就。连接池配置完毕后用的是 静态方法 调用的连接池
public class ABean
{
public static Connection getConn() { Connection conn = null;
try {
//驱动拷贝到lib中 Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/51ey");
//类型转换
DataSource ds = (DataSource) obj;
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace(); }
return conn;
} */
}我们在bean 也是些静态方法。在方法中获得连接 关闭连接 bean中全是这样的方法比如public class gqBean {
//首页会展信息
public static Vector getTjHz()
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null; conn=ABean.getConn();
vec=new Vector();
try{
stmt = conn.prepareStatement("select top 5 id,tp from hz where tj_bs='Y' order by a17 desc,a26 desc ");
rs = stmt.executeQuery();
while (rs.next()) {
HzForm list = new HzForm();
list.setId(rs.getInt("id"));
list.setTp(rs.getString("tp"));
vec.add(list);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return vec;
}}这鞋有毛病吗:请大家指点
还有如果不用连接池在SERVER2000 的分离数据库上看连接数是没有的
连接池到底是什么原理。用不用手写个清除连接的类
public class ABean
{
public static Connection getConn() { Connection conn = null;
try {
//驱动拷贝到lib中 Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/51ey");
//类型转换
DataSource ds = (DataSource) obj;
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace(); }
return conn;
} */
}我们在bean 也是些静态方法。在方法中获得连接 关闭连接 bean中全是这样的方法比如public class gqBean {
//首页会展信息
public static Vector getTjHz()
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null; conn=ABean.getConn();
vec=new Vector();
try{
stmt = conn.prepareStatement("select top 5 id,tp from hz where tj_bs='Y' order by a17 desc,a26 desc ");
rs = stmt.executeQuery();
while (rs.next()) {
HzForm list = new HzForm();
list.setId(rs.getInt("id"));
list.setTp(rs.getString("tp"));
vec.add(list);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return vec;
}}这鞋有毛病吗:请大家指点
还有如果不用连接池在SERVER2000 的分离数据库上看连接数是没有的
连接池到底是什么原理。用不用手写个清除连接的类
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货