import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class test {
//主要已经注册的驱动进行卸载
private static final Map<String,Driver> tempmap=new HashMap<String,Driver>();
public static void main(String[] args) {
//log.info("开始查询驱动信息");、
Connection conn=null;
Statement statement=null;
ResultSet rs =null;
String fileurl="d:\\ojdbc6.jar";
try{
//URL路劲获取本地驱动包
URL u = new URL("file:"+fileurl);
//将jar包路劲转为路劲类加载
URLClassLoader ucl = new URLClassLoader(new URL[] { u });
//静态加载驱动
Driver d = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver", true, ucl).newInstance();
//所有的获取的驱动都要必须实现Driver接口
driverImpl driver = new driverImpl(d);
//注册驱动
DriverManager.registerDriver(driver);
tempmap.put(fileurl, driver);
System.out.println("==============="+driver+"驱动正在加载...");
conn=DriverManager.getConnection("jdbc:oracle:thin:@29.22.22.137:1521:orcl", "test", "123456");
if(conn!=null){
String sql="select * from CZYTH_DATARESTORE";
statement = conn.createStatement();
rs = statement.executeQuery(sql);
while(rs.next()){
String string = rs.getString("HFLJ").toString();
System.out.println(string);
}
}
}catch(Exception e){
e.printStackTrace();
try {
DriverManager.deregisterDriver(tempmap.get(fileurl));
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
if(statement!=null||rs!=null){
statement.close();
rs.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
DriverManager.deregisterDriver(tempmap.get(fileurl));
//tempmap.remove(fileurl);
System.out.println("==============="+tempmap.get(fileurl));
Enumeration<Driver> drivers = DriverManager.getDrivers();
Driver sd = drivers.nextElement();
System.out.println(sd);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
public class driverImpl implements Driver{private Driver driver;
driverImpl(Driver d){
this.driver=d;
System.out.println(this.driver);
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
// TODO Auto-generated method stub
return this.driver.connect(url, info);
}
@Override
public boolean acceptsURL(String url) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
// TODO Auto-generated method stub
return this.getPropertyInfo(url, info);
}
@Override
public int getMajorVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getMinorVersion() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean jdbcCompliant() {
// TODO Auto-generated method stub
return false;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return this.getParentLogger();
}
}