gpt4 book ai didi

java - 我无法通过 Android SDK 连接到我的远程 MySQL

转载 作者:行者123 更新时间:2023-11-30 22:51:28 24 4
gpt4 key购买 nike

我有两个类似的程序。第一个:PC 版 Java,第二个:Android。我的本地网络中有一台 MacOS Pro 服务器,IP 为 192.168.0.103。 MacOS Server 已经有了 MySQL-Server 5.0.1,带有

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 应用程序执行此操作时,我遇到了连接失败的情况。我不知道为什么...我在下面发布了我的应用程序的完整代码:

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 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

我没有其他错误。

最佳答案

模拟器总是与你机器的网络隔离,看看这个Network Address Space

关于java - 我无法通过 Android SDK 连接到我的远程 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com