I am trying to connect the Sql Server 2008 R2 with java using JDBC.I have downloaded the jdbc jar files and i added in the eclipse.When i try to connect to sql 2008 R2 it says the following error.I am using the default port 1433. whether i have to change setting on the sql side.
This is my code.
package SocketClient;
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class SocketClient {
public static void main(String[] args) {
// Declare the JDBC objects.
Connection con = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
// Establish the connection.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("sa");
ds.setPassword("password123");
ds.setServerName("ENMEDIA-EA6278E\\ENMEDIA");
ds.setPortNumber(1433);
ds.setDatabaseName("DishTV_Voting");
con = ds.getConnection();
// Execute a stored procedure that returns some data.
cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");
cstmt.setInt(1, 50);
rs = cstmt.executeQuery();
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println("EMPLOYEE: " + rs.getString("LastName") +
", " + rs.getString("FirstName"));
System.out.println("MANAGER: " + rs.getString("ManagerLastName") +
", " + rs.getString("ManagerFirstName"));
System.out.println();
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
System.exit(1);
}
}
}
the error which i am getting while connecting to sql is
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host ENMEDIA-EA6278E, port 1433 has failed. Error: “Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:577)
at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57)
at SocketClient.SocketClient.main(SocketClient.java:23)
Can anyboby point me where i went wrong.Thanks in advance.Any tutorial for connecting sql with java along with installation of
Check if SQL Server is configured (via Surface Area Configuration) to accept remote TCP/IP connections.