无法从 Sql Server 2005 获取数据(连接超时异常) [英] Unable to get data from Sql Server 2005 (connection time out exception)

查看:41
本文介绍了无法从 Sql Server 2005 获取数据(连接超时异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

java.sql.SQLException:网络错误 IOException:连接超时:在 net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:410) 在 net.sourceforge.jtds.jdbc.ConnectionJDBC3.(ConnectionJDBC3.java:50) 在 net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) 在 java.sql.DriverManager.getConnection(DriverManager.java:582) 在 java.sql.DriverManager.getConnection(DriverManager.java:185) at mahesh.MyFrame.connectToServer(MyFrame.java:50) at mahesh.DataCount.main(DataCount.java:18) 由:java.net.ConnectException:连接超时:在 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)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 在 java.net.Socket.connect(Socket.java:519) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 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个

谁能帮我摆脱这个异常?

这是我的java代码

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

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

解决方案

作为

  • 用户是否有足够的权限连接数据库?帮助 2
  • 在您之前回答过这些问题后,再告诉我任何问题.

    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");**

    解决方案

    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.

    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.

    这篇关于无法从 Sql Server 2005 获取数据(连接超时异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆