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();
}
}
}