日期:2014-05-16  浏览次数:20435 次

终于实现动态加载JDBC驱动-oralce
终于实现动态加载JDBC驱动-oralce
2007-06-08 20:56

配置文件:

oracle.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.2.67:1521:xt
jdbc.user=sa
jdbc.password=sa

连接数据库类:

//package com.conn;
//
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Driver;
import java.util.Properties;

public class Conn2 {
private String dbUrl=null;
private String theUser=null;//用户名
private String thePw =null;//密码
private Connection c=null;//
private Statement conn;
private ResultSet rs=null;
private String driver = null;
private File jdbcDriverJarFileLocation = new File("d:\\oracle\\ora92\\jdbc\\lib\\ojdbc14.jar");

public Conn2(){
???? try {
????? //ORACLE的JDBC驱动类
????? Properties pro = new Properties();
????? try {
?????? pro.load(new FileInputStream("oracle.properties"));
????? } catch (FileNotFoundException e1) {
?????? // TODO Auto-generated catch block
?????? e1.printStackTrace();
????? } catch (IOException e1) {
?????? // TODO Auto-generated catch block
?????? e1.printStackTrace();
????? }
????? //System.out.println(pro.getProperty("jdbc.driver"));
????? dbUrl = pro.getProperty("jdbc.url");
????? theUser=pro.getProperty("jdbc.user");
????? thePw=pro.getProperty("jdbc.password");
????? driver=pro.getProperty("jdbc.driver");
????? System.out.println("1 "+driver);
????? try {
?????? URL jdbcDriverURL = jdbcDriverJarFileLocation.toURL();
?????? URL[] urls = new URL[1];
?????? urls[0] = jdbcDriverURL;
?????? URLClassLoader urlclassLoader = new URLClassLoader( urls,ClassLoader.getSystemClassLoader());??
?????? try {
??????? java.sql.Driver driverd = (java.sql.Driver)urlclassLoader.loadClass(driver).newInstance();
??????? System.out.println(driverd.toString());
??????? Properties????? props????? =????? new????? Properties();???
?????????????????????? props.setProperty("user",????? theUser);???
?????????????????????? props.setProperty("password", thePw???? );
??????? try {
???????? c = driverd.connect(dbUrl, props);
??????? } catch (SQLException e) {
???????? // TODO Auto-generated catch block
???????? e.printStackTrace();
??????? }
?????
?????? } catch (InstantiationException e) {
??????? // TODO Auto-generated catch block
??????? e.printStackTrace();
?????? } catch (IllegalAccessException e) {
??????? // TODO Auto-generated catch block
??????? e.printStackTrace();
?????? }//
????
????
????? } catch (MalformedURLException e1) {
?????? // TODO Auto-generated catch block
?????? e1.printStackTrace();
????? }
??
????? try {
????? conn=c.createStatement();
???? } catch (SQLException e) {
????? // TODO Auto-generated catch block
????? e.printStackTrace();
???? }
} catch (ClassNotFoundException e) {
???? // TODO Auto-generated catch block
???? e.printStackTrace();
}
}
//度曲配置文件。
public ResultSet executeQuery(String sql){
???? rs = null;
???? try {
????? rs = conn.executeQuery(sql);
???? } catch (SQLException e) {
????? // TODO Auto-generated catch block
????? e.printStackTrace();
???? }
???? return rs;
??
}

public void close(){
???? try {
????? conn.close();
????? c.close();
???? } catch (SQLException e) {
????? // TODO Auto-generated catch block
????? e.printStackTrace();
???? }
??
??
}

public static void main