现在的位置: 首页 > 综合 > 正文

一个很好用的java操作数据库连接池的类

2012年05月03日 ⁄ 综合 ⁄ 共 2557字 ⁄ 字号 评论关闭

/*
 * Created on 2007-1-11
 */
package wangyq.datasource;

/**
 * @author wangyaqiang
 * @version 1.0
 * @link http://niceboy.cnblogs.com
 */
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class MySqlDataSource {

 
 public Connection conn;
 public ResultSet rs;
 public Statement stmt;
   
 private static DataSource ds;
 /**
  *
  * 2007-1-12
  * wangyq
  *
  */
 public MySqlDataSource() {
  try
  {
     Context ctx = new InitialContext();
     if(ctx == null )
       throw new Exception("No Context");
 
     ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
 
     if (ds != null) {
     conn = ds.getConnection();
          
    if(conn != null)  {
    
    }
     }
  }catch(Exception e) {
     e.printStackTrace();
  }

 }
 
 public DataSource getDataSource()
 {
  try
  {
     Context initCtx = new InitialContext();
     if(initCtx == null )
     throw new Exception("No Context");
     Context envCtx=(Context)initCtx.lookup("java:comp/env");
     ds = (DataSource)envCtx.lookup("jdbc/mysql");

     if (ds != null) {
     return ds;
     }
     return null;
  }catch(Exception e) {
   e.printStackTrace();
   return null;
  }
 }
 /**
  *
  * @return
  *
  */
 public Connection getConnection()
 {
  try
  {
   return ds.getConnection();
  }
  catch(Exception e)
  {
   e.printStackTrace();
   return null;
  }
 }
 /**
  *
  * @param sql
  * @return
  */
 public ResultSet executeQuery(String sql)
   {
  rs=null;
  try
  {
   stmt=conn.createStatement();
   rs=stmt.executeQuery(sql);
  } 
  catch(SQLException ex)
  {
   System.err.println("MySqlDataSource.executeQuery():"+ex.getMessage());
  }
  return rs;
   }
   /**
    *
    * @param sql
    *
    */
    public void executeUpdate(String sql)
    {
   stmt=null;
   rs=null;
   try
   {
    stmt=conn.createStatement();
    stmt.executeUpdate(sql);
    stmt.close();
    conn.close();
   }
   catch(SQLException ex)
   {
    System.err.println("MySqlDataSource.executeQuery():"+ex.getMessage());
   }
    }
    /**
     *
     * @return
     *
     */
    public DatabaseMetaData getDbmd()
    {
   conn=null;
   DatabaseMetaData dbmd=null;
   try
   {
    dbmd=conn.getMetaData();
   }
   catch(SQLException ex)
   {
    System.err.println("MySqlDataSource.executeQuery():"+ex.getMessage());
   }
   return dbmd;
    }
    /**
     *
     * @return
     *
     */
   public boolean CloseConnection()
   {
   try
   {
    conn.close();
    return true;
   }
   catch(SQLException e)
   {
    e.printStackTrace();
    return false;
   }
   }
   /**
    *
    * @return
    *
    */
   public boolean CloseStmt()
   {
   try
   {
    stmt.close();
    return true;
   }
   catch(SQLException e)
   {
    e.printStackTrace();
    return false;
   }

   }
}

使用时需要根据自己的项目稍微修改一下
Context envCtx=(Context)initCtx.lookup("java:comp/env");
 ds = (DataSource)envCtx.lookup("jdbc/mysql");
改成自己的配置路径就可以了。当然也可以用参数把路径参给这个类,不过我很懒,能用就行了。

抱歉!评论已关闭.