gpt4 book ai didi

android - 使用 JDBC 从 Android 连接到 MySQL

转载 作者:IT老高 更新时间:2023-10-29 00:11:20 25 4
gpt4 key购买 nike

我使用以下代码从 Android 连接 localhost 中的 MySQL。它仅显示 catch 部分中给出的操作。不知道是不是连接问题。

package com.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test1Activity extends Activity {
/** Called when the activity is first created. */
String str="new";
static ResultSet rs;
static PreparedStatement st;
static Connection con;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv=(TextView)findViewById(R.id.user);

try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root","");
st=con.prepareStatement("select * from country where id=1");
rs=st.executeQuery();
while(rs.next())
{
str=rs.getString(2);


}


tv.setText(str);
setContentView(tv);
}
catch(Exception e)
{
tv.setText(str);
}
}
}

当这段代码执行时,它会在 avd 中显示“new”。

java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom

谁能提出一些解决方案?并提前感谢

最佳答案

您无法从 Android 本地访问 MySQL 数据库。编辑:实际上您可能能够使用 JDBC,但不推荐(或可能不起作用?)...见 Android JDBC not working: ClassNotFoundException on driver

http://www.helloandroid.com/tutorials/connecting-mysql-database

http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html

Android cannot connect directly to the database server. Therefore we need to create a simple web service that will pass the requests to the database and will return the response.

http://codeoncloud.blogspot.com/2012/03/android-mysql-client.html

对于大多数 [优秀] 用户来说,这可能没问题。但是想象一下,你遇到了一个黑客,他掌握了你的程序。我已经反编译了我自己的应用程序,而且我所看到的很可怕。如果他们将您的用户名/密码获取到您的数据库并造成严重破坏怎么办?不好。

关于android - 使用 JDBC 从 Android 连接到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12233145/

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