2012-02-18 124 views
2

在android中使用mysql連接通過jdbc在java中成功完成登錄表單...它在我的本地主機上成功工作...但發展網站沒有工作...以下錯誤顯示。jdbc連接socketexception錯誤

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) 

爲什麼發生錯誤發生。給我一些解決方案。 如果正在運行本地主機意味着我的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開發網站的用戶名,密碼,數據庫名稱和網址都僅改變R是正確的。 ..爲什麼DIS錯誤發生....給我解決方案...

+1

可以ping到該主機?你能通過其他工具連接到MySQL嗎? – 2012-02-18 13:09:14

+0

雅我平主機...然後只把...沒有..在這裏jdbc只用... – hariprasad 2012-02-18 13:28:48

+0

嘗試連接使用其他工具,並嘗試縮小問題搜索空間 – 2012-02-18 13:29:47

回答

0

聽起來像一個網絡問題。驗證您是否可以從正在建立連接的設備訪問數據庫主機和端口,以及DBMS是否已啓動並正在運行。

+0

dbms是什麼意思... bez數據庫主機和端口所有r只在我的連接正確... – hariprasad 2012-02-18 13:30:04

+0

「數據庫管理系統」。如果你能ping通它,也許是一個DNS問題。你有沒有嘗試在你的JDBC URL中使用IP? – 2012-02-18 13:37:53

+0

你也檢查...但沒有用.. – hariprasad 2012-02-18 13:52:21

0

關鍵問題是當您嘗試連接到數據庫時移動設備是否在網絡上。如果您在網絡上,則需要與MySQL數據庫管理員交談,看看您是否有權訪問該數據庫。我猜你沒有,因爲你試圖以root身份登錄而沒有密碼。沒有一位管理員的頭腦會讓你做到這一點。

如果您是管理員,請致電您的藥劑師的治療師。

如果移動設備不在網絡上,我猜測數據庫在某種防火牆後面。充其量,您必須與某位管理員交談才能設置防火牆規則,以允許從移動設備進行訪問。這些通常需要關於設備的一些信息。如果您打算爲多個用戶發佈應用程序,防火牆規則將不實用。

更好的解決方案是設置一個代理來驗證傳入的移動設備並代表它們與數據庫進行通信。

我不會有直接連接到數據庫的移動設備,特別是您嘗試去做的方式。

誰還知道你在做什麼?它看起來像你試圖連接到一個數據庫,但這個事實並不廣爲人知。誰還知道你的項目?

1

如果您嘗試從android應用程序連接到本地主機中的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。

0

如果您使用en模擬器使用一個特殊的IP到您的本地主機。

10.0.2.2

jdbc:mysql://10.0.2.2:3306/people