0

Hello I have this simple java code running in Eclipse in mac, just trying to connect to my google cloud database.

 public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection connection = DriverManager.getConnection("jdb:mysql://XX.XXX.X.XXX/test","root","1234");
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery("select * from cars");
    while(rs.next())
    {
        System.out.println(rs.getString(1)); 
    }
    connection.close();
  }

}

The problem I have is that when I use the newInstance function, Eclipse crosses it out, and when I run my program I get the Error:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdb:mysql://XX.XXX.X.XXX/test

Any help solving this issue would be greatly appreciated!

Daniel
  • 23
  • 6
  • 1
    The "crossed out" means that the method is deprecated and shouldn't be used any more. I'm not sure about the error. (`Class.newInstance` is deprecated in Java 9 and above, see [here](https://docs.oracle.com/javase/10/docs/api/java/lang/Class.html#newInstance()) for more info) – apetranzilla Apr 15 '18 at 01:05
  • @apemanzilla Could you post this as an answer, please? I would probably include the description below "Deprecated" in the link you posted. – Rubén C. Apr 23 '18 at 11:02
  • @RubénC. I didn't post it as an answer because it doesn't answer the main question about the exception, it just explains what the "crossed out" means. – apetranzilla Apr 23 '18 at 15:40
  • @apemanzilla I thought your suggestion could solve the issue but as a matter of fact Class.forName(...).newInstance() didn't throw an exception. – Rubén C. Apr 24 '18 at 07:34

1 Answers1

1

I think that the problem in your code is that you lack a “c” in “jdb”. Replace:

Connection connection = DriverManager.getConnection("jdb:mysql://XX.XXX.X.XXX/test","root","1234");

with:

Connection connection = DriverManager.getConnection("jdbc:mysql://XX.XXX.X.XXX/test","root","1234");

Verify that you have also imported the JAR files into Eclipse as described in this related old SO case.

As @apemanzilla said in his comment, the cross out means that the method is deprecated. The following lines explain how to update it:

The call class.newInstance() can be replaced by class.getDeclaredConstructor().newInstance()

Rubén C.
  • 1,098
  • 6
  • 16