1

I was trying to connect to ms sql server 2008 which is installed on a remote system.But it showing error.The following is the way i had tried

import java.io.File;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
import java.sql.Statement;  


public class mssql {  
    public static void main(String[] args) {  
        try {  
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

            Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
            if(!(connection==null))
            {
                System.out.println("connected");
            }

//            


        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

This is the error i am getting

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.220, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at mssql.main(mssql.java:14)

Any solutions for this problem

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Can you access the remote server via SQL Server Management Studio or "telnet 192.168.1.220 1433"? – Rainer.R Sep 23 '13 at 12:11
  • @Rainer.R It say can not open connection – SpringLearner Sep 23 '13 at 12:39
  • @Rainer.R my windows firewall was blocking it,now i am able to telnet. But i can not connect to sql server – SpringLearner Sep 23 '13 at 13:03
  • "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and **accepting TCP/IP connections at the port**. Make sure that TCP connections to the port **are not blocked by a firewall**." – Mark Rotteveel Sep 23 '13 at 13:03

4 Answers4

1

Is the 2008 SQL instance configured to be listening for TCP connections?

Start, Microsoft SQL Server 2008, Configuration Tools, SQL Server Configuration Manager SQL Server Network Configuration
Protocols for [instance name]
There should be four items listed:

  • Shared Memory
  • Named Pipes
  • TCP/IP
  • VIA

For your environment, which should be enabled and which should be disabled? Most setups call for Shared Memory and TCP/IP enabled, the others disabled.

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
jdev
  • 5,304
  • 1
  • 17
  • 20
0
public class mssql {  
    public static void main(String[] args) {  
        try {  
            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); modify
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

            Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
            if(!(connection==null))
            {
                System.out.println("connected");
            }

//            


        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

Then you look at this post:

JDBC: Simple MSSql connection example not working

Community
  • 1
  • 1
sunysen
  • 2,265
  • 1
  • 12
  • 13
  • It shows this error java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) – SpringLearner Sep 23 '13 at 12:43
  • Then you look at this post:http://stackoverflow.com/questions/12523865/jdbc-simple-mssql-connection-example-not-working – sunysen Sep 23 '13 at 12:47
0

Try to connect from the command line:

sqlcmd -S 192.168.1.220 -E

Also, check this:

  • Is the server firewall configured (allow port 1433 or sqlservr.exe);
  • Is your instance name correct? If you are using SQL Server express, the server name would be 192.168.1.220\sqlexpress;
  • Are TCP connections enabled for the server (see answer from jdev);
  • Are remote connections enabled for the server (can be changed in SQL Server Management Studio, Server properties, Connections);
  • Is port 1433 correct? By default, only unnamed instances use static port 1433. Other instances use a dynamic port and require SQL Server Browser for discovery.
cdoubleplusgood
  • 1,309
  • 1
  • 11
  • 12
0

-first enable your sql server to receive tcp connection from configuration manager. -add sqljdbc library to project. - add the .dll file of the library as vm argument ex: -Djava.library.path=...... -use the code below as example:

 String connectionUrl = "jdbc:sqlserver://localhost;" +
     "databaseName=Timesheet;integratedSecurity=true;";
    Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {

     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     con = DriverManager.getConnection(connectionUrl);


     String SQL = "select top 100 * from paycal";
     stmt = con.createStatement();
     rs = stmt.executeQuery(SQL);
     int i =0;

     while (rs.next()) {

        jTable1.setValueAt(rs.getString("dayname"), i, 0);
        jTable1.setValueAt(rs.getString("dater"), i, 1);
        i++;

     }
G.Nader
  • 847
  • 7
  • 9