jdbc连接socketexception错误

时间:2012-02-18 13:07:04

标签: java jdbc exception-handling

我在android中使用mysql连接通过java中的jdbc成功完成登录表单...它在我的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)

为什么会出现错误错误。给我一些解决方案。 如果我正在运行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开发网站的用户名,密码,数据库名称和网址都只是正确的...那么为什么会出现错误....给我解决方案......

4 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

关键问题是当您尝试连接数据库时移动设备是否在网络上。如果您在网络上,则需要与MySQL数据库管理员联系,以查看您是否有权访问该数据库。我猜你没有,因为你试图以root用户身份登录而没有密码。没有正确思想的管理员会允许你这样做。

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

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

更好的解决方案是设置一个代理,该代理将对传入的移动设备进行身份验证,并代表他们与数据库进行通信。

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

还有谁知道你要做什么?它看起来像你在尝试连接数据库,但这个事实并不广为人知。还有谁知道你的项目?

答案 3 :(得分:0)

如果您使用en emulator,请使用特殊IP到您的localhost。

10.0.2.2

jdbc:mysql://10.0.2.2:3306/people