2015-01-20 56 views
1

連接到我的遠程MySQL我有兩個類似的計劃。第一個:Java適用於PC,第二個:Android。我的本地網絡中有一臺IP地址爲192.168.0.103的MacOS Pro服務器。 MacOS的服務器已經得到的MySQL服務器5.0.1,與我無法通過Android SDK中

CREATE DATABASE db_demo01; 
CREATE USER 'user01'@'%' IDENTIFIED BY '1234567'; 
GRANT ALL ON db_demo01.* TO 'user01'@'%'; 

我的PC應用程序有以下代碼:

  String driver = "com.mysql.jdbc.Driver"; 
      String url = "jdbc:mysql://192.168.0.103:3306/"; 
      String dbName = "db_demo01"; 
      String userName = "user01"; 
      String userPass = "1234567"; 

      try 
      { 
       Class.forName(driver); 
       Connection conn = DriverManager.getConnection(url+dbName,userName,userPass); 
       System.out.println("Connected!"); 
       conn.close(); 
      } 
      catch(Exception ex) 
      { 
       System.out.println("Error:" + ex.getMessage()); 
      } 

這是工作,但是當我試圖用我的Android應用程序要做到這一點,我」! v連接失敗。我不知道爲什麼......我下面貼我的應用程序的全碼:

package com.navi.newser; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 

import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

    /// VARIABLES 
    private TextView textWidget; 
    private TableLayout tableWidget; 

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

     CreateOnClickConnect(); 
    } 

    private void CreateOnClickConnect() 
    { 
     textWidget = (TextView)findViewById(R.id.textView1); 
     tableWidget = (TableLayout)findViewById(R.id.table1); 
     Button cmd = (Button)this.findViewById(R.id.button3); 
     cmd.setOnClickListener(new View.OnClickListener() { 

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

    private class Connect extends AsyncTask<String, String, String> { 
     @Override 
     protected String doInBackground(String... urls) 
     { 
      String response = ""; 

      String driver = "com.mysql.jdbc.Driver"; 
      String url = "jdbc:mysql://192.168.0.103:3306/"; 
      String dbName = "db_demo01"; 
      String userName = "user01"; 
      String userPass = "1234567"; 
      Connection conn = null; 
      try 
      { 
       Class.forName(driver).newInstance(); 
       conn = DriverManager.getConnection(url+dbName,userName,userPass); 
       response += "Connected!"; 
       Log.e("MySQL", response); 
       conn.close(); 
      } 
      catch(Exception ex) 
      { 
       ex.printStackTrace(); 
       response += "Error:" + ex.getMessage(); 
       Log.e("MySQL", response); 
      } 
      publishProgress("Almost..."); 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      textWidget.setText(result); 
     } 

     @Override 
     protected void onProgressUpdate(String... text) { 
      textWidget.setText(text[0]); 
     } 
    } 

    public void ConnectToMySQL() 
    { 
     new Connect().execute(); 
    } 

enter image description here

我使用Eclipse月神。

01-20 19:09:07.777: E/dalvikvm(1069): Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo 

我沒有其他錯誤。

+0

我建議你檢查mysql的設置,主要是爲了「綁定」的地址,如果你的服務器只監聽localhost,那麼就不會接受來自不同的IP連接,althoug我不知道,如果Android模擬器擁有自己的IP – 2015-01-20 19:39:46

回答

0

與此

emulator -avd avdname -http-proxy http://192.168.0.1:8080

啓動模擬器這裏更換avdname您要運行AVD的名字程序和http:192.168.1.1到您的代理服務器。

original answer

0

那麼你必須做出一些設置;首先模擬器並不總是一個好主意,而中間是互聯網連接。此外,連接器的庫(jar)應該位於Android項目中名爲lib的文件夾中。對於MySQL交互,我使用JSON和PHP。我希望這對你有所幫助!