0

I am trying to write code for bringing a text file's data into a database using Eclipse, MySQL Workbench, and JDBC 8.0.11. It is giving me a ClassNotFoundException. I have looked at multiple other questions, and they have all been fixed by putting the java\com\mysql\jdbc\Driver.java inside the DriverManager.getConnection parameter. I have already done that, and it is still giving me an error. Anyone have any ideas as to why I'm still getting this error?

public static void main(String[] args) throws Exception{
    Class.forName                                                                   //Register JDBC Driver
    ("*mysql-connector-java-8.0.11.\\src\\legacy\\java\\com\\mysql\\jdbc\\Driver.java*")
    .newInstance();  

    conn =      DriverManager.getConnection (url, user, pass);   
    Statement stmt = conn.createStatement();
    String mysql1 = "UPDATE Policy SET " + readAndArray                         //Changeable file path
            ("filepath"); 

    }

NEW EDIT

Following @zlakad 's advice, it turns out that you don't need to use Class.forName() if you have Java 6 or higher. Although, now I have a new error: SQLNonTransientConnectionException because of the underlying WrongArgumentException. I'm puzzled as to why it does this because I'm not using the incorrect parameters for DriverManager.getConnection. Any suggestions?

String url = "file path";         //Changeable for MySQL DB
String user = "root";
String pass = "password";

public static void getConnection() throws Exception { 

    Connection conn = DriverManager.getConnection(url, user, pass);   
    Statement stmt = conn.createStatement();
hpotter_otter
  • 61
  • 1
  • 5

3 Answers3

1

You have to load driver class for connection not jar file of that class

you shoud try this:

Class.forName("com.mysql.jdbc.Driver");
The Impaler
  • 45,731
  • 9
  • 39
  • 76
Prashant Sonar
  • 113
  • 1
  • 2
  • 10
1

Try this:

// None of this belongs in a main method. 
public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");     
    // where are url, user, pass set?  I don't see them.   
    Connection conn = DriverManager.getConnection(url, user, pass);
    Statement stmt = conn.createStatement();
    // this is simply wrong.
    String mysql1 = "UPDATE Policy SET " + readAndArray("filepath");
}

You're new to Java and JDBC. This is not a good way to do it. I'd recommend that you search the web and SO for some examples of how to do it better.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

I was using the wrong format for a database url in the DriverManager.getConnection();

I changed my url to a jdbc:mysql://host:3306/ and it worked.

String url = "jdbc:mysql://*host*:3306/";
Connection conn = DriverManager.getConnection(url, user, pass);
hpotter_otter
  • 61
  • 1
  • 5