帮兄弟看看,这样调用会不会有问题.

lclmdj 2004-09-30 11:53:52
连接连接池的class


package com.wisdom.system.DB;

import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

public class ConnectionFactory {
private static DataSource ds;
static{
try {
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/jdbc/bn");
}
catch (NamingException e) {
System.out.println("lclException_1="+e.toString());
}
}
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
public static void closeConnection(Connection conn){
if(conn!=null){
try{
conn.close();
}
catch(SQLException e)
{System.out.println("lclException_2="+e.toString());}
}
}
}

调用连接池的类

package com.wisdom.system.homesite;

import java.util.*;
import java.sql.*;
import com.wisdom.system.DB.ConnectionFactory;

public Vector getAdvList() throws Exception {
Vector advListVt = new Vector();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = ConnectionFactory.getConnection();
String sql ="SELECT advid,adv_name,adv_url,adv_sort FROM adver order by adv_sort";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] a = new String[2];
a[0] = rs.getString("adv_name");
a[1] = rs.getString("adv_url");
advListVt.add(a);
}
}
catch (SQLException e) {System.out.print("lclException_3="+e.toString());}
finally {
try {
if (rs != null) {rs.close();rs = null;}
if (stmt != null){stmt.close();stmt = null;}
if (conn != null) {ConnectionFactory.closeConnection(conn);}
}
catch (Exception e) {System.out.print("lclException_4="+e.toString());}
}
return advListVt;
}
public Vector getDirecList() throws Exception {
Vector direcListVt = new Vector();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// MyConnection conn = new MyConnection();
// con = conn.getConnection();
String sql =
"SELECT direc_id,direc_name,direcall_name,direc_url,direc_sort,onshow,"
+ "class,ischild,isproduct,pdate,ishomepagepro FROM director "
+ "where class=1 and onshow=1 order by direc_sort";
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] a = new String[5];
if((String) rs.getString("direc_name") != null && !((String) rs.getString("direc_name")).equals("")){
a[0] = (String) rs.getString("direc_name").toString().trim();}else{a[0] = "";}
if((String) rs.getString("direc_url") != null && !((String) rs.getString("direc_url")).equals("")){
a[1] = (String) rs.getString("direc_url").toString().trim();}else{a[1] = "";}
if((String) rs.getString("direc_id") != null && !((String) rs.getString("direc_id")).equals("")){
a[2] = (String) rs.getString("direc_id").toString().trim();}else{a[2] = "";}
if((String) rs.getString("class") != null && !((String) rs.getString("class")).equals("")){
a[3] = (String) rs.getString("class").toString().trim();}else{a[3] = "";}
if((String) rs.getString("ishomepagepro") != null && !((String) rs.getString("ishomepagepro")).equals("")){
a[4] = (String) rs.getString("ishomepagepro").toString().trim();}else{a[4] = "";}
direcListVt.add(a);
}
}
catch (SQLException e) {System.out.print("lclException_5="+e.toString());}
finally {
try {
if (rs != null) {rs.close(); rs = null;}
if (stmt != null) { stmt.close(); stmt = null;}
if (conn != null) { ConnectionFactory.closeConnection(conn); }
}
catch (Exception e) {System.out.println("lclException_8="+e.toString());}
}
return direcListVt;
}
...全文
37 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lclmdj 2004-09-30
if (conn != null) { ConnectionFactory.closeConnection(conn); }

空指针到是没发现,你看我这么关库行吗?
因为这里面我没NEW一个对象,而是直接关掉了上一个连库的class
这样会不会把整个库关掉,当其它方法在调用时会不会出错.
回复
shangqiao 2004-09-30
只看代码的话,我觉得没有什么问题,但是我觉得还是作一下空指针检测比较好
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告