I get the following error when trying to execute a Java program with SQL code:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/opiejbc1
I have installed the driver mysql-connector-j-8.0.31.jar in /usr/share/java
I have called the class with java -cp ./:/usr/share/java TestApplication and with CLASSPATH=./:/usr/share/java set.
My Java code is as follows:
import java.sql.*;
public class TestApplication {
   static final String DB_URL = "jdbc:mysql://localhost/opiejbc1";
   static final String USER = "opiejbc1";
   static final String QUERY = "SELECT * FROM Test1";
   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, "");
         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();
      } 
   }
}
I have tried inserting Class.forName("com.mysql.cj.jdbc.Driver"); immediately after the try statement, but then I get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Syntax error on token(s), misplaced construct(s)
        at TestApplication.main(TestApplication.java:10)
What am I doing wrong?
I have tried all the recommended solutions as far as I know, but I still get those errors.
 
     
    