Android JDBC 不工作:驱动程序上的 ClassNotFoundException [英] Android JDBC not working: ClassNotFoundException on driver

查看:48
本文介绍了Android JDBC 不工作:驱动程序上的 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用程序中使用 JDBC 连接到远程数据库以执行插入、查询等操作.我已成功连接并在不同的 JAVA 项目中完成了这些操作.所以我想既然 Android 是 Java,我可以移植相关代码,为驱动程序添加相同的构建路径等.但它给了我错误:

I'm trying to use JDBC in my Android application to connect to a remote database to do inserts, queries, etc. I have successfully connected and done these things in a different JAVA project. So I figured since Android is Java, I could just port over the relevant code, add the same build path for the driver, etc. But it gives me the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

我真的不认为这是代码问题,因为相同的代码在 Java 项目中工作(我只是在 main() 中执行).但这里的参考是:

I really don't think it's a code issue since the same code works in a Java project (which I just execute in main()). But for reference here it is:

String url = "jdbc:mysql://localhost:3306/eventhub_test"; //
    String user = "root"; 
    String pass = "";

 SQLUtils sqlu = new SQLUtils(url, user, pass);

//我创建的SQLUtils类:

//the SQLUtils class I made:

public class SQLUtils {


private String CONNECTION_URL;
private String user;
private String pass;
private java.sql.Statement stmt; 
private java.sql.Connection conn;

public SQLUtils(String conn_url, String user, String pass) {
    this.CONNECTION_URL = conn_url; 
    this.user = user;
    this.pass = pass; 
}


public void init() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {

    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
    stmt = conn.createStatement();

}
}

所以我真的很困惑.JDBC 不适用于 Android 吗?如果是这样,请告诉我应该为远程 MySQL 数据库访问寻找哪些替代方案.

So I'm really confused here. Does JDBC not work with Android? If so, tell me what alternatives I should look into for remote MySQL database access.

谢谢.

推荐答案

JDBC 不适用于 Android 吗?

Does JDBC not work with Android?

JDBC 在 Android 上很少使用,我当然不会推荐它.

JDBC is infrequently used on Android, and I certainly would not recommend it.

恕我直言,JDBC 是为高带宽、低延迟、高度可靠的网络连接而设计的(例如,桌面到数据库服务器、Web 应用程序服务器到数据库服务器).移动设备提供的这些很少,而且没有一个始终如一.

IMHO, JDBC is designed for high-bandwidth, low-latency, highly-reliable network connections (e.g., desktop to database server, Web application server to database server). Mobile devices offer little of these, and none of them consistently.

如果是这样,请告诉我我应该寻找哪些替代方法来访问远程 MySQL 数据库.

If so, tell me what alternatives I should look into for remote MySQL database access.

围绕您的数据库创建一个 Web 服务并从 Android 访问该服务.

Create a Web service around your database and access that from Android.

作为附带好处,您可以提高安全性(相对于让数据库保持打开状态),可以从客户端卸载一些业务逻辑,可以更好地支持其他平台(例如,Web 或基于 Web 的移动框架)等.

As side benefits, you improve security (vs. leaving your database open), can offload some business logic from the client, can better support other platforms (e.g., Web or Web-based mobile frameworks), etc.

这篇关于Android JDBC 不工作:驱动程序上的 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆