2017-06-27 38 views
-2

你好我是初學者android.I想要在我的電腦上建立一個數據庫連接到mssql服務器。我在網上找到了一個例子。 我想我在連接ip或端口時出了點問題。我猜語法是錯誤的。 我得到這個日誌:ANDROID DATABASE - 網絡錯誤IOException

網絡錯誤IOException:無法連接到/127.0.0.1(1433端口)從/ ::(端口55062):連接失敗:ECONNREFUSED(連接被拒絕)

import android.annotation.SuppressLint; 
import android.os.StrictMode; 
import android.util.Log; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
public class ConnectionClass { 
String ip = "127.0.0.1:1433"; 
String classs = "net.sourceforge.jtds.jdbc.Driver"; 
String db = "DBAndroid1"; 
String un = "TestUser"; 
String password = "123"; 

這是我的MainActivity.java。

public class MainActivity extends AppCompatActivity { 

ConnectionClass connectionClass; 
EditText edtuserid, edtpass; 
Button btnlogin; 
ProgressBar pbbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    connectionClass = new ConnectionClass(); 
    edtuserid = (EditText) findViewById(R.id.et_username); 
    edtpass = (EditText) findViewById(R.id.et_password); 
    btnlogin = (Button) findViewById(R.id.btn_Login); 
    pbbar = (ProgressBar) findViewById(R.id.pbbar); 
    pbbar.setVisibility(View.GONE); 

    btnlogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      DoLogin doLogin = new DoLogin(); 
      doLogin.execute(""); 

     } 
    }); 

} 


public class DoLogin extends AsyncTask<String,String,String> 
{ 
    String z = ""; 
    Boolean isSuccess = false; 


    String userid = edtuserid.getText().toString(); 
    String password = edtpass.getText().toString(); 


    @Override 
    protected void onPreExecute() { 
     pbbar.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected void onPostExecute(String r) { 
     pbbar.setVisibility(View.GONE); 
     Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show(); 

     if(isSuccess) { 
      Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show(); 
     } 

    } 

    @Override 
    protected String doInBackground(String... params) { 
     if(userid.trim().equals("")|| password.trim().equals("")) 
      z = "Please enter User Id and Password"; 
     else 
     { 
      try { 
       Connection con = connectionClass.CONN(); 
       if (con == null) { 
        z = "Error in connection with SQL server"; 
       } else { 
        String query = "select password from User"; 
        Statement stmt = con.createStatement(); 
        ResultSet rs = stmt.executeQuery(query); 

        if(rs.next()) 
        { 

         z = "Login successfull"; 
         isSuccess=true; 
        } 
        else 
        { 
         z = "Invalid Credentials"; 
         isSuccess = false; 
        } 

       } 
      } 
      catch (Exception ex) 
      { 
       isSuccess = false; 
       z = "Exceptions burda mi "; 
      } 
     } 
     return z; 
    } 
} 
} 



@SuppressLint("NewApi") 
public Connection CONN() { 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    Connection conn = null; 
    String ConnURL = null; 
    try { 

     Class.forName(classs).newInstance(); 
     ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" 
       + "databaseName=" + db + ";user=" + un + ";password=" 
       + password + ";"; 
     conn = DriverManager.getConnection(ConnURL); 
    } catch (SQLException se) { 
     Log.e("ERRO0", se.getMessage()); 
    } catch (ClassNotFoundException e) { 
     Log.e("ERRO1", e.getMessage()); 
    } catch (Exception e) { 
     Log.e("ERRO2", e.getMessage()); 
    } 
    return conn; 
} 
} 

我希望你能幫上忙。謝謝。

+0

但我從那裏不明白。我爲什麼要這樣做? – trial

+0

什麼部分你不明白? –

+0

你是從模擬器還是你的設備做到這一點?如果是您的設備,請確保您使用的是Wi-Fi,而不是使用手機數據。它需要在同一個網絡上。如果您正在使用模擬器,則當Android模擬器位於VM上時,您的IP應爲10.0.2.2而非localhost(又名127.0.0.1)。 – Carson

回答

0

你是從模擬器還是你的設備做到這一點?在您的設備上,確保您使用的是Wi-Fi,而不是使用手機數據。它需要在同一個網絡上。您的IP應該是10.0.2.2而不是localhost(又名127.0.0.1),因爲Android模擬器位於VM上。由於模擬器的本地主機指的是模擬器本身,而不是真正的本地主機。

+0

是的,謝謝你的工作。但現在我有另一個錯誤。它說「關鍵字用戶附近的語法不正確」。 – trial

+0

這聽起來像使用MS SQL的保留字的錯誤https://stackoverflow.com/questions/25589031/50894-error-reading-incorrect-syntax-near-the-keyword-user-when-tries-to- sele – Carson

+0

Okey我用[User]。它的工作。感謝您的幫助。 – trial