[英]Cannot find the JDBC driver
我有一個簡單的JDBC代碼。
static Connection c;
static PreparedStatement ps;
public static void initializeDB() throws IOException, ClassNotFoundException, SQLException {
Properties prop = new Properties();
prop.load(new FileInputStream("dbconn.properties"));
String connurl = prop.getProperty("connurl");
String driver = prop.getProperty("driver");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(driver); //prints "com.mysql.jdbc.Driver"
Class.forName(driver);
c = DriverManager.getConnection(connurl, username, password);
但是我越來越
java.lang.ClassNotFoundException:“ com.mysql.jdbc.Driver”(位於java.net.URLClassLoader $ 1.run(URLClassLoader.java:202),位於java.security.AccessController.doPrivileged(本機方法),位於java.net.URLClassLoader。在java.lang.ClassLoader.loadClass(ClassLoader.java:307)的findClass(URLClassLoader.java:190)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)的java.lang.ClassLoader.loadClass(ClassLoader的) .java:248),位於java.lang.Class.forName0(本機方法),位於testapp.DBUpdater.initializeDB(Testapp.java:71),位於java.lang.Class.forName(Class.java:169)。主要(Testapp.java:38)
從print語句可以很好地訪問屬性值。 當我直接用字符串值替換變量時,它可以正常工作!
我什么時候應該使用
prop.load(new FileInputStream(System.getProperty(“ dbconn.properties”)));;
當我從mysql-connector jar文件中查看Driver類時,我期望看到一些靜態代碼,但沒有找到任何東西。
看起來該字符串的值為"com.mysql.jdbc.Driver"
(請注意雙引號),而不是com.mysql.jdbc.Driver
。
刪除那些引號,它應該起作用。
嘗試修剪屬性的值。
我將以靜態方式加載屬性。
我不明白你為什么要在這個JAR中看一眼...
例:
class Database {
private final static Properties properties;
private static Connection c;
private static PreparedStatement ps;
static {
properties = new Properties();
properties.load(new FileInputStream("dbconn.properties"));
}
public static void init() {
String connurl = properties.getProperty("connurl").trim();
String driver = properties.getProperty("driver").trim();
String username = properties.getProperty("username").trim();
String password = properties.getProperty("password").trim();
Class.forName(driver);
c = DriverManager.getConnection(connurl, username, password);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.