JDBC-数据库连接工具类

package com.ycx.util;

import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.sql.PooledConnection;
public class ConnectionManager {
  /*private static final String DRIVER_CLASS="oracle.jdbc.driver.OracleDriver";
  private static final String DATABASE_URL="jdbc:oracle:thin:@localhost:1521:orcl";
  private static final String DATABASE_USER="scott";
  private static final String DATABASE_PASSWORD="flf";*/
/*    private static final String DRIVER_CLASS="oracle.jdbc.driver.OracleDriver";
      private static final String DATABASE_URL="jdbc:oracle:thin:@localhost:1521:orcl";
      private static final String DATABASE_USER="scott";
      private static final String DATABASE_PASSWORD="flf";*/
    private static final String DRIVER_CLASS="com.mysql.jdbc.Driver";
      private static final String DATABASE_URL="jdbc:mysql://localhost/ytx?useUnicode=true&characterEncoding=utf8";
      private static final String DATABASE_USER="root";
      private static final String DATABASE_PASSWORD="flf";
  /**
   * 返回连接
   * @ return Connection
   */
  public static Connection getConnection(){
      Connection conn=null;
      try{
          Class.forName(DRIVER_CLASS);
          conn=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
      }catch(Exception e){
          e.printStackTrace();
      }
      return conn;
  }
 
  public static Connection getPoolConnection() throws NamingException{
       DataSource ds = null;
       Connection conn = null;
       InitialContext ctx =   new InitialContext();   
       try{  
           ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");  
           conn = ds.getConnection(); 
        //   ctx = new InitialContext();
        } catch(SQLException e){
           e.printStackTrace();
       }
      return conn;
  }
  /**
   * 关闭连接
   * @param dbConnection
   *        Connection
   */
  public static void closeConnection(Connection conn){
      try{
        if(  conn!=null&&(!conn.isClosed())){
              conn.close();
          }
      }catch(SQLException sqlEx){
          sqlEx.printStackTrace();
      }
  }
  /**
   * 关闭结果集
   * @param res
   *    ResultSet
   */
  public static void closeResultSet(ResultSet rs){
      try{
        if(rs!=null){
            rs.close();
            rs=null;
        }
      }catch(SQLException e){
          e.printStackTrace();
      }
  }
  /**
   * 关闭语句
   * @param pStatement
   *      @ PreparedStatement
   */
  public static void closePreparedStatement(PreparedStatement pstmt){
      try{
          if(pstmt!=null){
              pstmt.close();
              pstmt=null;
          }
      }catch(SQLException e){
          e.printStackTrace();
      }
  }
 
  public static void closeStatement(Statement pstmt){
      try{
          if(pstmt!=null){
              pstmt.close();
              pstmt=null;
          }
      }catch(SQLException e){
          e.printStackTrace();
      }
  }
 
 
}
/**
 * public static synchronized Connection getConnection() throws DBAccessException{
 *   String driverClassName=Env.getInstance().getProperty("driver");
 *   String url=Env.getIstance().getProperty("url");
 *   String password=Env.getIstance().getProperty("password");
 *   String username=Env.getIstance().getProperty("username");
 *   Connection con=null;
 *   try{
 *    Class.forName(driverClassName);
 *    conn=DriverManager.getConnection(url,user,password);
 *   }catch(Exception e){e.printStackTrace()};
 * }return conn
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值