Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

java - Unable to get data from Sql Server 2005 (connection time out exception)

java.sql.SQLException: Network error IOException: Connection timed out: connect at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:410) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at mahesh.MyFrame.connectToServer(MyFrame.java:50) at mahesh.DataCount.main(DataCount.java:18) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:307) at net.sourceforge.jtds.jdbc.SharedSocket.(SharedSocket.java:257) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:311) ... 6 more

Can anyone help me in getting rid of this exception?

here is my java code

**Class.forName("net.sourceforge.jtds.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://mindmill:1433/employ","mahesh","mahesh");**

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the jTDS FAQ states, the URL must be in the form

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

Infering from your connection:

  • mindmill is the name of your_computer/server where Sql Server 2005 is installed.
  • 1433 is the (default) port to connect with Sql Server 2005.
  • employ is the database name.
  • mahesh is your user and password to connect to server.

From here on, you must set other sql connection parameters. I'll post you my code:

package edu.jtds.main;

import java.sql.*;

public class SqlServerConnTest {

    Connection conn;

    public void connect() {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            String dbName = "TestDB";
            String user = "cajeroUpz";
            String password = "cajero";
            //the name of my SQL SERVER 2005 instance
            String SqlServerInstance = "instance=SQL2005";
            String url = "jdbc:jtds:sqlserver://localhost:1433";
            url = url + "/" + dbName;
            url = url + ";" + SqlServerInstance;
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return this.conn;
    }

    public static void main(String[] args) {
        SqlServerConnTest oSqlServerConnTest = new SqlServerConnTest();
        oSqlServerConnTest.connect();
        String sql = "SELECT * FROM TEST_TABLE";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = oSqlServerConnTest.getConnection(); 
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while(rs.next()) {
                System.out.println(String.format("%d %s", 
                    rs.getInt(1), rs.getString(2)));
            }
            rs.close();
            stat.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The output of my program:

1 hello world
2 goodbye!

The lessons here:

  1. From SQL Server 2005 and on, you must set the name of the instance (like the example above).
  2. In SQL Server, you must check that your TCP/IP protocol is enabled and the communication port is 1433 (this last is setted by default, just check it). You can enable/disable using SQL Server Configuration Management in the SQL Server 2005 Configuration Tools.

Checking TCP/IP protocol for SQL Server 2005

Any other problems, just tell me.

EDIT:

The best case would be if you have already tried this connection with your pc as a Server, I mean your pc must have Sql Server 2005 installed, NetBeans installed and the project already setted up and running (as a proof of concept to connect the database).

Even if you haven't do the step before, there is a set of questions you should have answered before trying to connect a remote server:

  1. Have you checked communication between your pc and the host? In your case, prompt a Command Line (Start / Run... type 'cmd' and Enter) and enter the command "ping mindmill", check the hostname of your server.
  2. Have you installed Sql Server Management Studio in your pc and connected to your server? does your server allow remote connections? Help 1
  3. Does the user have enough privileges to connect the database? Help 2

Let me know any more issues after you have answered this questions before.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...