Android的JDBC不工作:ClassNotFoundException的驾驶员驾驶员、工作、Android、JDBC

2023-09-11 11:30:37 作者:丢不掉

我想使用JDBC在我的Andr​​oid应用程序连接到远程数据库执行插入,查询等,我已成功连接,并在不同的Java项目做这些事情。所以我想既然Android是Java的,我可以在相关的code口,添加相同的构建路径驱动程序等,但它给我的错误:

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

我真的不认为这是因为在一个Java项目相同的code作品code的问题(这是我的主只需要执行())。但对于参考这里是:

 字符串URL =的jdbc:mysql的://本地主机:3306 / eventhub_test; //
    字符串用户=根;
    字符串传递=;

 SQLUtils sqlu =新SQLUtils(URL,用户通过);
 

//在SQLUtils类我做:

 公共类SQLUtils {


私人字符串CONNECTION_URL;
私人字符串的用户;
私人字符串传递;
私人java.sql.Statement中的语句;
私人java.sql.Connection中的康涅狄格州;

公共SQLUtils(字符串conn_url,用户字符串,字符串传递){
    this.CONNECTION_URL = conn_url;
    this.user =用户;
    this.pass =通;
}


公共无效的init()抛出IllegalAccessException,InstantiationException,ClassNotFoundException的,的SQLException {

    的Class.forName(com.mysql.jdbc.Driver)的newInstance()。
    康恩=的DriverManager.getConnection(CONNECTION_URL,用户通过);
    语句= conn.createStatement();

}
}
 
安卓使用jdbc连接mysql 数据库 Android使用JDBC连接数据库实现增 删 该 查 操作 5.0版本 ...

所以我真的很困惑在这里。难道JDBC不与Android合作?如果是的话,告诉我,我应该考虑的远程MySQL数据库的访问有什么替代方案。

感谢。

解决方案   

难道JDBC不与Android合作?

JDBC很少使用在Android上,我肯定不会推荐它。

IMHO,JDBC设计用于高带宽,低延迟,高可靠的网络连接(例如,桌面到数据库服务器,Web应用服务器向数据库服务器)。移动设备几乎无法提供这些,并且他们没有始终如一。

  

如果是这样,告诉我,我应该考虑的远程MySQL数据库的访问有什么替代方案。

在你的数据库中创建一个Web服务并访问来自Android系统。

由于方的利益,你提高安全性(相对于离开你打开数据库),可以卸载一些业务逻辑从客户端,可以更好地支持其他平台(如网络或基于Web的移动架构),等等。

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

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

//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();

}
}

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.

Thanks.

解决方案

Does JDBC not work with Android?

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

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.

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

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

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.