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)

