我目前正在需要加載mysql驅動運行時並使用java連接到數據庫的需求。加載並連接到mysql jdbc驅動運行時
我使用URLClassLoader
加載jar文件
File f = new File("D:/Pallavi/workspace/WarInstallation/mysql-connector-java-5.0.4-bin.jar"); //Jar path
URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL()},System.class.getClassLoader());
Class sqldriver = urlCl.loadClass("com.mysql.jdbc.Driver"); // Runtime loading
Driver ds = (Driver) sqldriver.newInstance(); //Compilation failing as "sqldriver" class of type Driver is not found
//I am using now java.sql.Driver to remove the compilation error
sqldriver = Class.forName("com.mysql.jdbc.Driver", true, sqldriver.getClassLoader()).newInstance(); //Runtime fail.. "Driver" Class not Found Exception.
雖然類加載罰款,我不能建立數據庫連接(中找不到合適的驅動程序...)不管我的驅動程序。
請建議加載jdbc「com.mysql.jdbc.Driver
」類運行時的方法。 讓我知道,如果您需要任何進一步的信息,因爲這是迫切的。
在此先感謝。
你有沒有在classpath mysql的罐子? –
你好,我已經在環境變量中設置了mysql jar的類路徑,我們是否需要通過系統屬性來設置它? – Pallavi