jdbc串連oracle資料庫

來源:互聯網
上載者:User

package com.hhwy.test ;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;

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

import org.apache.commons.dbcp.BasicDataSource;

 

public class DbTools
{
 private Connection conn = null ;
 private ResultSet rs = null;
 private Statement stmt = null;
 private String tempPath = "temp"; // 存放blob臨時檔案的位置
 private static final BasicDataSource datasource; 
 static {
  datasource = new BasicDataSource();
  datasource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
  datasource.setUrl("jdbc:oracle:thin:@192.168.0.90:1521:bjspdgis");
  datasource.setUsername("epmmanager");
  datasource.setPassword("manager");
 }
 
 public DbTools()
 {
  System.out.println("---------------------DbTools進入----") ;
  try{
  //conn = this.GetConnection() ;
   //conn = this.CreateConnection() ;
   conn = DbTools.GetDBPoolConnection("jdbc/DBPool") ;
   if(conn!=null)
   {
    this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
   }
  }catch(Exception e)
  {
   e.printStackTrace() ;
   
  }
  
 }
//tomcat串連池方式s
 private static Connection GetDBPoolConnection(String poolName)
 {
  Context c_Cont = null ;
  DataSource ds=null;
  try
  {
   c_Cont = new InitialContext();
  }catch(Exception e)
  {
   System.out.println("Couldn't build an initial context : " + e);
   return null;
  }
  try
  {
   ds = (DataSource)c_Cont.lookup("java:/comp/env/" + poolName);
  }catch(Exception e)
  {
   e.printStackTrace() ;
   return null ;
  }
  try
  {
   return ds.getConnection() ;
  }catch(SQLException e)
  {
   e.printStackTrace() ;
   System.out.println("SQLException in DataBean.getPoolConnection():"
     + e.getMessage() + " jndi is :" + poolName) ;
   return null ;
  }
 }
 
 //jdbc方式串連
 public Connection CreateConnection()
 {
  try
  {
   //DBPool dbp=new DBPool();
   Connection con = null ;
   String connName = "jdbc:oracle:thin:@192.168.0.90:1521:bjspdgis";
   // System.out.print("使用串連 getConnect():" + connName + "...");
   Class.forName("oracle.jdbc.driver.OracleDriver");
   con = DriverManager.getConnection(connName, "epmmanager",
     "manager");
   
   if(con!=null)
   {
    System.out.println("-------------串連成功!!!") ;
   }else{
    
    System.out.println("----------------串連失敗!!!") ;
   }
   return con ;
  }catch(Exception e)
  {
   e.printStackTrace() ;
  }
  return null ;
  
 }
 
 public Connection GetConnection()
 {
  Connection conn1 = null;
  try {
   conn1 = datasource.getConnection();
  } catch (Exception e) {
   e.printStackTrace();
  }

  return conn1;
  
 }
 
 public ResultSet executeQuery(String sql) throws SQLException {
 
  try
  {
  System.out.println(sql);
  rs = stmt.executeQuery(sql);
  }catch(Exception e)
  {
   e.printStackTrace() ;
  }

  return rs;
 }
 public void CloseDB()
 {
  try
  {
   datasource.close() ;
  }catch(Exception e)
  {
   e.printStackTrace() ;
  }
  
 }

 public static void shutdownDbcp() throws Exception {
  System.out.println("串連池關閉 datasource.close()");
  datasource.close();
  
  
 }
 public void closecon()
 {
  try
  {
   conn.close() ;
  } catch (SQLException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.