简体   繁体   中英

Error java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/opiejbc1

I'm having trouble accessing MySql from Java.

I'm using:

Operating system : Ubuntu 20.04.5 LTS (GNU/Linux 5.15.79.1-microsoft-standard-WSL2 x86_64)

mysql Ver 8.0.31-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

javac 11.0.17

I use the following compilation line:

 javac -cp /usr/share/java/mysql-connector-j-8.0.31.jar TestApplication.java

mysql-connector-j-8.0.31.jar is the mysql-connector.jar

I use the following line to execute:

java TestApplication

One of the lines produced by jar tf /usr/share/java/mysql-connector-j-8.0.31.jar

com/mysql/cj/jdbc/Driver.class

My source code:

    import java.sql.*;

    public class TestApplication {
       static final String DB_URL = "jdbc:mysql://localhost:3306/opiejbc1";
       static final String USER = "opiejbc1";
       static final String PWD = "Linux@Me1";
       static final String QUERY = "SELECT * FROM Test1";
       static final String DRIVER = "com.mysql.cj.jdbc.Driver";
   
       public static void main(String[] args) {
          // Open a connection
          try {
             Connection conn = DriverManager.getConnection(DB_URL, USER, PWD);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(QUERY);
             // Extract data from result set
             while (rs.next()) {
                // Retrieve by column name
                System.out.print("Name: " + rs.getInt("Name"));
                System.out.print(", Phone: " + rs.getInt("Phone"));
             }
          } catch (SQLException e) {
             e.printStackTrace();
          }   
       }
    }

On execution I received:

 java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/opiejbc1

I've also tried the following:

    import java.sql.*;

    public class TestApplication {
       static final String DB_URL = "jdbc:mysql://localhost:3306/opiejbc1";
       static final String USER = "opiejbc1";
       static final String PWD = "Linux@Me1";
       static final String QUERY = "SELECT * FROM Test1";
       static final String DRIVER = "com.mysql.cj.jdbc.Driver";
   
       public static void main(String[] args) {
           // Open a connection
          try {
             Class.forName(DRIVER);
             Connection conn = DriverManager.getConnection(DB_URL, USER, PWD);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(QUERY);
             // Extract data from result set
             while (rs.next()) {
                // Retrieve by column name
                System.out.print("Name: " + rs.getInt("Name"));
                System.out.print(", Phone: " + rs.getInt("Phone"));
             }
          } catch (SQLException e) {
             e.printStackTrace();
          } catch (ClassNotFoundException e) {
             e.printStackTrace();
          }
        }
    }

On execution I receive the:

 java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

What am I doing wrong?

Ensure your mysql connector jar is on your classpath when running the code.

Edit considering all the comments:

If java TestApplication delivers java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver , then try this from the same directory:

java -cp /home/opiejbc1/java:/usr/share/java/mysql-connector.jar:. TestApplication

Note the :. in the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM