数据库工具连接数类

package com.accp.BaseDao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/*
 * 数据库工具连接类
 */
public class BaseDao {
  
  private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
  private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:jbit";
  private static final String USER = "system";
  private static final String PWD = "123456";
 public Connection context(){
  Connection conn = null;
  DataSource ds = null;
  try {
   Context ctx = new InitialContext();
   ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Oracle");
    
  } catch (NamingException e) {
   e.printStackTrace();
  }
   try {
    conn = ds.getConnection();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  return conn;
  }
  
     
  
 public static Connection getConnection() throws SQLException,ClassNotFoundException{
  Connection conn = null;
  Class.forName(DRIVER);
  conn = DriverManager.getConnection(URL, USER, PWD);
  
  
  return conn;
  
 }
 public static void close(ResultSet rs,Statement pt, Connection conn){

  try {
   if (rs != null) {
    rs.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   if (pt != null) {
    pt.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   if (conn != null) {
    conn.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();

  }
 }
    

 }

 

你可能感兴趣的:(java,数据库链接)