通過URLClassLoader實現動態載入jar檔案
阿新 • • 發佈:2019-01-23
Java程式碼
java.net
- package t;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.Driver;
- import java.util.Properties;
- publicclass Test2 {
- publicstaticvoid main(String[] args) throws Exception {
- String url = "jdbc:sqlserver://localhost:1433;DatabaseName=loushang"
- String user = "sa";
- String password = "loushang";
- String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- String driverJar = "file:/D:/Desktop/sqljdbc4.jar";
- URL urls[] = new URL[] { new URL(driverJar)};
- URLClassLoader loader = new
- Class<?> clazz = loader.loadClass(driverClass);
- Driver driver = (Driver)clazz.newInstance();
- Properties p = new Properties();
- p.put("user", user);
- p.put("password", password);
- Connection con = driver.connect(url, p);
- DatabaseMetaData meta = con.getMetaData();
- System.out.println("getDatabaseProductName()="+meta.getDatabaseProductName());
- System.out.println("getDatabaseProductVersion()="+meta.getDatabaseProductVersion());
- System.out.println("getDefaultTransactionIsolation()="+meta.getDefaultTransactionIsolation());
- System.out.println("getDriverName()="+meta.getDriverName());
- con.close();
- }
- }
package t;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.util.Properties;
public class Test2 {
public static void main(String[] args) throws Exception {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=loushang";
String user = "sa";
String password = "loushang";
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String driverJar = "file:/D:/Desktop/sqljdbc4.jar";
URL urls[] = new URL[] { new URL(driverJar)};
URLClassLoader loader = new URLClassLoader(urls);
Class<?> clazz = loader.loadClass(driverClass);
Driver driver = (Driver)clazz.newInstance();
Properties p = new Properties();
p.put("user", user);
p.put("password", password);
Connection con = driver.connect(url, p);
DatabaseMetaData meta = con.getMetaData();
System.out.println("getDatabaseProductName()="+meta.getDatabaseProductName());
System.out.println("getDatabaseProductVersion()="+meta.getDatabaseProductVersion());
System.out.println("getDefaultTransactionIsolation()="+meta.getDefaultTransactionIsolation());
System.out.println("getDriverName()="+meta.getDriverName());
con.close();
}
}
輸出結果:
Java程式碼- getDatabaseProductName()=Microsoft SQL Server
- getDatabaseProductVersion()=8.00.760
- getDefaultTransactionIsolation()=2
- getDriverName()=Microsoft SQL Server 2005 JDBC Driver
getDatabaseProductName()=Microsoft SQL Server
getDatabaseProductVersion()=8.00.760
getDefaultTransactionIsolation()=2
getDriverName()=Microsoft SQL Server 2005 JDBC Driver
關於URLClassLoader:
java.net
類 URLClassLoader
直接已知子類:MLet
public class URLClassLoader
extends SecureClassLoader該類載入器用於從指向 JAR 檔案和目錄的 URL 的搜尋路徑載入類和資源。這裡假定任何以 '/' 結束的 URL 都是指向目錄的。如果不是以該字元結束,則認為該 URL 指向一個將根據需要開啟的 JAR 檔案。