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?

GeertPt
  • 16,398
  • 2
  • 37
  • 61

4 Answers4

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.

JHS
  • 7,761
  • 2
  • 29
  • 53
1

First load the driver class

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

then create connections.

Sumit Gupta
  • 437
  • 4
  • 12
  • That's done. But this is for sqlserver authentication. and mine is windows authentication. How to connect with that? – user2718433 Oct 02 '13 at 10:05
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().

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
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.

GeertPt
  • 16,398
  • 2
  • 37
  • 61