Reputation: 9
I want to connect to a database using Java
. I use this code:
public static Connection getConnection() throws Exception {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:sqlserver://localhost;integratedSecurity=true;";
String username = "root";
String password = "mysql";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
return conn;
} catch(Exception e) {
System.out.println(e);
}
return null;
}
But I don't know the correct default url
and driver
property values. I use MySQLWorkBench
.
Upvotes: 0
Views: 860
Reputation: 460
Your driver is Class.forName("com.mysql.jdbc.Driver");
and your url will be Connection con=DriverManager.getConnection("jdbc:mysql://{hostname}:3306/{schemaname}","{username}","password");
and you can download the mysql jdbc driver from here
Hoping you would find this useful. Also remember 3306 is the default port and your MySQL instance should be running on this port. All the best :-)
Upvotes: 0
Reputation: 6574
the Driver you are using is correct but you will need to download include mysql connector jar in your build path.
For the url the mysql is by default running on port 3306 so your url will be like this, replace [yourdatabasename] with the name of your database, useSSL is set since the connection is not https
jdbc:mysql://localhost:3306/[yourDatabasename]?useSSL=false
Upvotes: 1