Reputation: 2735
I am trying to connect a Java program to a database in localhost. My problem seems to be very simple, but I cannot find any answer.
When I try to compile, I get the following error:
DriverManager.getConnection cannot be resolved to a type
On the following code:
try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
System.out.println("Error found " + ex);
ex.printStackTrace();
}
// Here is where the error pops up
Connection connection = new DriverManager.getConnection("jdbc:mysql//localhost:3306/project3", "root", "root");
I am assuming I screwed up some installation, but I am not completely sure what.
Thank you for any help.
Upvotes: 2
Views: 12307
Reputation: 1123
Just Edit your code like this
try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
System.out.println("Error found " + ex);
ex.printStackTrace();
}
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/project3", "root", "root");
Avoid new And put : after jdbc:mysql
Upvotes: 1
Reputation: 35557
why new
here?
Just use
DriverManager.getConnection("jdbc:mysql//localhost:3306/project3",
"root", "root");
getConnection()
is static
method. just call it as above.
Upvotes: 6