0

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?

3

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

First load the driver class

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/test","monty","greatsqldb");

then create connections.

1 Comment

That's done. But this is for sqlserver authentication. and mine is windows authentication. How to connect with that?
0

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().

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.