我有两个类似的程序。第一个: Java for PC ,第二个: Android 。我的本地网络中有一台带有IP 192.168.0.103的MacOS Pro Server。 MacOS Server已经获得了MySQL-Server 5.0.1,
CREATE DATABASE db_demo01;
CREATE USER 'user01'@'%' IDENTIFIED BY '1234567';
GRANT ALL ON db_demo01.* TO 'user01'@'%';
我的电脑应用程序有以下代码:
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应用程序执行此操作时,我的连接失败了。我不知道为什么......我在下面发布了我的应用程序的完整代码:
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();
}
我使用Eclipse Luna。
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 :(得分:0)
模拟器始终与您的计算机网络隔离,请查看此Network Address Space
答案 1 :(得分:0)
使用此
启动您的模拟器 emulator -avd avdname -http-proxy http://192.168.0.1:8080
此处将avdname替换为您要运行程序的avd名称,并将http:192.168.1.1替换为您的代理服务器。
答案 2 :(得分:0)
嗯,你必须做一些设置;首先,使用模拟器并不总是一个好主意,而中间有互联网连接。此外,连接器的库(jar)应位于Android项目中名为lib的文件夹中。对于MySQL交互,我使用JSON和PHP。希望这对你有所帮助!