【问题标题】:jdbc connection socketexception errorjdbc连接socket异常错误
【发布时间】:2012-03-09 15:08:41
【问题描述】:

我通过java中的jdbc使用mysql连接成功完成了android中的登录表单......它在我的localhost中成功运行......但是开发站点它不起作用......显示以下错误。

java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection timed out: connect

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection timed out: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection timed out: connect
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.retrieve.retrieve.main(retrieve.java:15)


** END NESTED EXCEPTION **


    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.retrieve.retrieve.main(retrieve.java:15)

为什么会出现dis错误。给我一些解决方案。 如果正在运行 localhost 意味着我的 sql 查询是

             package com.example.login;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLayoutActivity extends Activity {


    EditText username,password;
    TextView error;
    Button ok;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        username=(EditText)findViewById(R.id.et_un);
        password=(EditText)findViewById(R.id.et_pw);
        error=(TextView)findViewById(R.id.tv_error);
        ok=(Button)findViewById(R.id.btn_login);

        ok.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Editable user = username.getText();
                Editable pass = password.getText();

                try {

                    **Class.forName("com.mysql.jdbc.Driver");

                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/people", "root", "");
                        Statement st = con.createStatement();


                        ResultSet rs = st.executeQuery("SELECT password FROM customers where login='"+user+"'");**

                        if(rs.next())
                        {
                            String dbpass = rs.getString(1);
                            if(dbpass.equals(pass)){
                                 error.setText("Correct Username or Password");

                        }
                        else
                        {
                            error.setText("Sorry!! Incorrect Username or Password");
                        }
                        }
                }

                     catch (SQLException e) {
                        username.setText(e.toString());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


        });
    }
}

如果正在运行开发站点意味着我的查询是:

              package com.example.login;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLayoutActivity extends Activity {


    EditText username,password;
    TextView error;
    Button ok;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        username=(EditText)findViewById(R.id.et_un);
        password=(EditText)findViewById(R.id.et_pw);
        error=(TextView)findViewById(R.id.tv_error);
        ok=(Button)findViewById(R.id.btn_login);

        ok.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Editable user = username.getText();
                Editable pass = password.getText();

                try {

                    **Class.forName("com.mysql.jdbc.Driver");

                        Connection con = DriverManager.getConnection("jdbc:mysql://dev.xxxxx.com/xxx", "xxxx", "xxxx");
                        Statement st = con.createStatement();


                        ResultSet rs = st.executeQuery("SELECT password FROM customers where login='"+user+"'");**

                        if(rs.next())
                        {
                            String dbpass = rs.getString(1);
                            if(dbpass.equals(pass)){
                                 error.setText("Correct Username or Password");

                        }
                        else
                        {
                            error.setText("Sorry!! Incorrect Username or Password");
                        }
                        }
                }

                     catch (SQLException e) {
                        username.setText(e.toString());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


        });
    }
}

dis 开发站点的用户名、密码、数据库名称和 url 都正确而已...那么为什么会出现 dis 错误....给我解决方案...

【问题讨论】:

  • 你能ping到那个主机吗?可以通过其他工具连接 MySQL 吗?
  • 我 ping 主机...然后只放...没有..这里只使用 jdbc...
  • 尝试使用其他工具连接,并尝试缩小问题搜索空间
  • 我开发了android原生应用程序...这里只能通过jdbc连接mysql...所以只问...任何其他解决方案

标签: java jdbc exception-handling


【解决方案1】:

如果您尝试从 android 应用程序连接 localhost 中的 mysql,它可能无法通信,refer this

这个异常似乎 mysql 没有公共访问权限。 使您的 mysql 在远程系统中可用并提供远程系统的 ip。设 192.168.1.102 为其 ip,然后将此语句更改为 jdbc:mysql://192.168.1.102:3306/people

你必须修改mysql配置文件。它可能是 /etc/mysql/my.cnf(在 linux 中)或 \xampp\mysql\bin\my.ini(取决于 mysql refer more)。 默认包含bind-address = 127.0.0.1,修改为bind-address = 0.0.0.0。进行此更改后,mysql 就可以公开使用,即可以远程访问它。现在您的应用程序可以访问这个 mysql。

【讨论】:

    【解决方案2】:

    听起来像是网络问题。验证您是否可以从正在建立连接的设备访问数据库主机和端口,以及 DBMS 是否已启动并正在运行。

    【讨论】:

    • dbms 是什么意思...bez 数据库主机和端口仅在我的连接中正确...
    • “数据库管理系统”。如果能ping通,可能是DNS问题。您是否尝试过在 JDBC URL 中使用 IP?
    【解决方案3】:

    关键问题是当您尝试连接到数据库时,移动设备是否在网络上。如果您在网络上,则需要与 MySQL 数据库管理员交谈,以查看您是否有权访问该数据库。我猜你没有,因为你试图在没有密码的情况下以 root 身份登录。没有一个头脑正常的管理员会允许你这样做。

    如果您是管理员,请致电您的药剂师治疗师。

    如果移动设备不在网络上,我猜测数据库位于某种防火墙后面。充其量您必须与某些管理员交谈以设置防火墙规则以允许从移动设备进行访问。这些通常需要有关设备的一些信息。如果您打算为多个用户发布应用程序,防火墙规则将不实用。

    更好的解决方案是设置一个代理来验证传入的移动设备并代表它们与数据库进行通信。

    我不会让移动设备直接连接到数据库,尤其是您尝试这样做的方式。

    还有谁知道您要做什么?在我看来,您正在尝试连接到数据库,但这一事实并不广为人知。还有谁知道你的项目?

    【讨论】:

      【解决方案4】:

      如果您使用 en 模拟器,请使用特殊 IP 连接到您的 localhost。

      10.0.2.2

      jdbc:mysql://10.0.2.2:3306/people

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-13
        • 2012-09-09
        • 2012-06-03
        • 1970-01-01
        • 2015-06-12
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        相关资源
        最近更新 更多