java to MS SQL 2005 connection

Hi good afternoon,

I am facing problem while connecting with MSSQL Server 2005 through java. I am only able to connect with server using windows authentication. and when I tried to connect it with SQL server authentication using username as "sa" and null password it is giving an error.

I have written the following code

import java.sql.*;

public class SqlServer{

public static void main(String[] args) throws Exception{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=hibernate;integratedSecurity=true";

Connection con = DriverManager.getConnection(connectionUrl);

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("select * from dbo.customers");

while(rs.next())

System.out.println(rs.getString("cust_id"));

rs.close();

stmt.close();

con.close();

System.in.read();

}

}

And the error is

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source

)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Sour

ce)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)

at java.sql.DriverManager.getConnection(DriverManager.java:525)

at java.sql.DriverManager.getConnection(DriverManager.java:171)

at SqlServer.main(SqlServer.java:11)

[1841 byte] By [deepak.parkhea] at [2007-11-27 2:27:13]
# 1
You haven't supplied the username and password to the url nor the getConnection().
BalusCa at 2007-7-12 2:37:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
that is what i am saying.Everytime I uses windows authentication while connecting to MS Sql Server Management Studio. so i also don't know what username and password to use. I tried "sa" and null but it not working.
deepak.parkhea at 2007-7-12 2:37:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
You can retrieve the username of the operating system environment by System.getProperty("user.name"). But you cannot retrieve the password belonging to this username.
BalusCa at 2007-7-12 2:37:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Thank you for your co-operation sir,I have got solution for this problem.I had just enabled the TCP/IP connection setting in Configuration Manager provided with SQL Server 2005.
deepak.parkhea at 2007-7-12 2:37:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> I had just enabled the TCP/IP connection setting

> in Configuration Manager provided with SQL Server

> 2005.

What did this solve exactly? I thought you need the uid/pwd of the logged in user of the client's operaring system.

Edit nevermind, this just solved the actual connection problem. But are you logging in without password then?

Message was edited by:

BalusC

BalusCa at 2007-7-12 2:37:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...