user2718433
user2718433

Reputation: 11

Connect Java with MySQL using Windows Authentication?

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

Answers (4)

GeertPt
GeertPt

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

Vimal Bera
Vimal Bera

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

Sumit Gupta
Sumit Gupta

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

JHS
JHS

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

Related Questions