Reputation: 11
Can anyone explain me how to connect Java with MySQL?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=monty&password=greatsqldb");
}
catch (SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
This is how i made it but mine is windows authentication and not password authentication. So how to complete the connection with windows authentication?
Upvotes: 0
Views: 1995
Reputation: 17846
I guess you use the MySQL External Authentication for Windows (http://www.mysql.com/products/enterprise/security.html).
This is supported in Connector/Net driver (.NET), using 'Integrated Security=yes' as connection string option: http://dev.mysql.com/doc/refman/5.5/en/connector-net-programming-authentication-windows-native.html
I can't find any documentation about support for this in the Connector/J reference (Java). I guess it's not supported in the Java driver.
Upvotes: 0
Reputation: 10497
Try this :
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","monty","greatsqldb");
You can specify the username
and password
seperately.
FYI : It is not necessary to load the driver using Class.forName() method but to be on safe side, coders use this method to load the drivers.
Read This. It is written that Applications no longer need to explictly load JDBC drivers using Class.forName().
Upvotes: 0
Reputation: 447
First load the driver class
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/test","monty","greatsqldb");
then create connections.
Upvotes: 1
Reputation: 7871
You are not loading the driver class.
Try this -
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?" + "user=monty&password=greatsqldb");
P.S. I have assumed you are using MySQL
.
Upvotes: 1