SQL Server Authentication

I am connection to two SQL servers of which one is SQL authentication and other is SQL authentication.

I have currently an application which connects to the SQL Server using SQL authentication

The method used to create an application is below:

publicstatic Connection GetSQLConnection( String server, String port, String db, String user, String password){

if (port ==null){

port ="1433";

}

String driverName ="com.microsoft.jdbc.sqlserver.SQLServerDriver";

String myDBServer = server +":" + port;

String trustedConn ="; trustedConnection=yes";

String url ="jdbc:microsoft:sqlserver://" + myDBServer.toString() +"; databaseName=" + db.toString() + trustedConn.toString();

System.out.println(url);

Connection connection =null;

try{

Class.forName(driverName);

connection = DriverManager.getConnection(url,user, password);

connection.setCatalog(db);

if ( connection !=null){

System.out.println("Connection Successful.");

}else{

System.out.println("Connection Failed.");

}

}catch(ClassNotFoundException cnfe){

// Could not find the database driver

cnfe.printStackTrace();

System.out.println("..no db driver." + cnfe.getMessage());

System.exit(1);

}catch(SQLException sqle){

// Could not connect to the database

sqle.printStackTrace();

System.out.println("..could not connect to db." + sqle.getMessage());

System.exit(1);

}//end of try catch block

return connection;

}// end method GetSQLConnection

I want to create a connection object which can authenticate the server using the Windows authentication.

Can someone help me edit the same method for Windows authentication?

[2935 byte] By [SirAnanda] at [2007-11-27 8:28:09]
# 1
My guess is that you need a different driver to do that.I believe the jTDS driver does that (see sourceforge.)
jschella at 2007-7-12 20:18:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

I rewrote the code after putting the jtds.jar fiel in my class path

public static Connection GetSQLConnection( String server, String port, String db, String user, String password){

if (port == null){

port = "1433";

}

String driverName = "net.sourceforge.jtds.jdbc.Driver";

String myDBServer = server + ":" + port;

String url = "jdbc:jtds:sqlserver://" + myDBServer.toString() + "/" + db.toString() ;

System.out.println(url);

Connection connection = null;

Properties property = new Properties(System.getProperties());

try{

Class driver = Class.forName(driverName);

connection = DriverManager.getConnection(url,property);

connection.setCatalog(db);

if ( connection != null){

System.out.println("Connection Successful.");

}else{

System.out.println("Connection Failed.");

}

}catch(ClassNotFoundException cnfe){

// Could not find the database driver

cnfe.printStackTrace();

System.out.println("..no db driver." + cnfe.getMessage());

System.exit(1);

}catch(SQLException sqle){

// Could not connect to the database

sqle.printStackTrace();

System.out.println("..could not connect to db." + sqle.getMessage());

System.exit(1);

}//end of try catch block

return connection;

}// end method GetSQLConnection

But now I am getting this exception:

at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:611)

at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:331)

at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)

at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:178)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at com.wellpoint.pdfcreator.SQLTools.GetSQLConnection(SQLTools.java:95)

at com.wellpoint.pdfcreator.PDFCreatorApp.main(PDFCreatorApp.java:40)

Caused by: java.io.IOException: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.

at net.sourceforge.jtds.jdbc.TdsCore.sendMSLoginPkt(TdsCore.java:1894)

at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:584)

... 7 more

Completed at 6/22/2007 12:59 PM

Could you please help me here?

SirAnanda at 2007-7-12 20:18:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> Caused by: java.io.IOException: SSO Failed: Native

> SSPI library not loaded. Check the java.library.path

> system property.

Which means you are missing something.

I would suppose that the docs for the driver would provide information about that.

Or that you could google about it.

jschella at 2007-7-12 20:18:01 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

The file I was missing was the ntlmauth.dll

I registered the dll file with the following code snippet before establishing the connection

try{

System.loadLibrary("ntlmauth");

System.out.println("The authentication library loaded sucessfully.");

}catch(UnsatisfiedLinkError err){

System.out.println("Unable to load authentication library: " + err);

}

Also the dll file needs to be in the classpath of the jar file.

Thank you so much for all your help and advice

Warm Regards,

Anand

SirAnanda at 2007-7-12 20:18:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...