JDBC — java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.Connection

后端 未结 2 1594
独厮守ぢ
独厮守ぢ 2021-01-17 01:37

I have the following code that\'s trying to establish a connection to a remote database. It\'s throwing an exception, and I can\'t figure out why.

    impor         


        
2条回答
  •  悲哀的现实
    2021-01-17 01:50

    Try this:

    try
    {   // Load driver class
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (java.lang.ClassNotFoundException e) {
        System.err.println("ClassNotFoundException: " +e);
    }
    

    Instead of:

     try {
        // Exception thrown in next line.
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            return null;
        }
    

提交回复
热议问题