Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1614198
  • 博文数量: 585
  • 博客积分: 14610
  • 博客等级: 上将
  • 技术积分: 7402
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-15 10:52
文章存档

2013年(5)

2012年(214)

2011年(56)

2010年(66)

2009年(44)

2008年(200)

分类:

2008-05-22 03:50:03


   auth="Container" 
  type="javax.sql.DataSource"/> 
  
    
    factory 
    org.apache.commons.dbcp.BasicDataSourceFactory 
   
 

 
    
    maxActive 
    100 
   
 

 
    
    maxIdle 
    100 
   
 

 
    
    maxWait 
    10000 
   
 
 
    
    username 
     
   
 
    
    password 
     
   
 

 
    
    driverClassName 
    sun.jdbc.odbc.JdbcOdbcDriver 
   
 

 
    
    url 
    jdbc:odbc:newslist 
   
 
 


=================
===============================================================
DBConnection .java

package news;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

//import com.AlertClass.Alert;

public class DBConnection {

 private DataSource ds = null;

 private Connection con = null;
 private ResultSet rs = null;
 public DBConnection() {

  try {

   Context ctx = new InitialContext();
   //搜索数据库的资源 封装了DriverManager,它的底层还是通过DriverManager来获取连接对象的
   ds = (DataSource) ctx.lookup("java:comp/env/jdbc/news2");

  } catch (NamingException e) {

   e.printStackTrace();
  }
 }

 public Connection getConnection() {

  try {
   con = ds.getConnection();

  } catch (SQLException e) {
   e.printStackTrace();
  }
  return con;
 }

 public void closeConnection() {
  try {

   if (this.con != null) {
    con.close();
   }

  } catch (SQLException sqlex) {
   sqlex.printStackTrace();
  }

 }

 public void closePrepStmt(PreparedStatement prepStmt) {
  try {
   if (prepStmt != null)
    prepStmt.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void closeResultSet(ResultSet rs) {
  try {
   if (rs != null)
    rs.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public ResultSet executeQuery(String sql) {
  try {
   //connect = DriverManager.getConnection("jdbc:odbc:newslist");  //??b????????????l??
   this.getConnection();
   Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
   rs = stmt.executeQuery(sql);//执行指定的数据查询语句
  }
  catch(SQLException ex) {
   ex.printStackTrace();
  }
  return rs;
 }
 /*public static Connection getConnection(){
 
  Connection con=null;
 
 
  ResourceBundle rb=ResourceBundle.getBundle("dataBaseProperties");
  String driverClassName=rb.getString("classForName");
  String url=rb.getString("url");
  String username=rb.getString("username");
  String password=rb.getString("password");
 
  try{
  Class.forName(driverClassName);
  con=DriverManager.getConnection(url,username,password);
  }catch(ClassNotFoundException cnfe){
  new Alert(cnfe.getMessage());
  System.out.println(cnfe.getMessage());
  } catch (SQLException sqle) {
  new Alert(sqle.getMessage());
  System.out.println(sqle.getMessage());
  }
  return con;  
  }*/
}
 
阅读(1257) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~