帮兄弟看看,这样调用会不会有问题.
连接连接池的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;
}