Reputation: 23
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!
Upvotes: 0
Views: 70
Reputation: 1098
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 byclass.getDeclaredConstructor().newInstance()
Upvotes: 1