Android + MySQL使用com.mysql.jdbc.Driver

我正在编写一个将连接到MySQL服务器的Android应用程序。 现在,我正在使用http:// localhost:3306 /通过XAMPP在我的计算机上测试MySQL服务器。 以下代码在严格测试JAVA应用程序时工作正常。

import java.sql.*; public class MySQL{ public static void main(String[] args) { System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "database"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Connected to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } } 

但是当我将它添加到我的Android应用程序时,我得到了Exception e错误。

 // interact with MySQL!!! private View.OnClickListener onSendRequest = new View.OnClickListener() { @Override public void onClick(View v) { EditText username = (EditText) findViewById(R.id.Huusername); // String un = " " + username.getText().toString() + " "; System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "database"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); Toast.makeText(getBaseContext(), "Connected to the database.", Toast.LENGTH_LONG) .show(); conn.close(); Toast.makeText(getBaseContext(), "Disconnected form the database.", Toast.LENGTH_LONG) .show(); } catch (Exception e) { Toast.makeText(getBaseContext(), "Exception e.", Toast.LENGTH_LONG) .show(); e.printStackTrace(); } } }; 

在编译我的Android应用程序时,我注意到控制台中的以下语句:

 [2011-01-26 16:05:54 - hookup]: Dxwarning: Ignoring InnerClasses attribute for an anonymous inner class (com.mysql.jdbc.interceptors.ResultSetScannerInterceptor$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. 

我认为这个陈述与我得到的exception有关。 有没有人使用MySQL和Android可以引导我朝着正确的方向发展? 谢谢

使用JDBC连接器mysql-connector-java-3.0.17-ga-bin.jar,现在是唯一有效的连接器。

检查你的代码,你需要为你的应用程序的每个活动实现一个连接池,只需添加一个私有的“连接”var并在你的OnCreate方法中初始化,如con = DriveManager.getConnection ……然后使用它每次需要访问MYSQL时都会使用private var。

另一种方法是使用使用三层体系结构的虚拟JDBC驱动程序:您的JDBC代码通过HTTP发送到远程Servlet,该Servlet在将JDBC代码(配置和安全性)传递给MySql JDBC驱动程序之前对其进行过滤。 结果通过HTTP发回。 有一些免费软件使用这种技术。 只是Google“基于HTTP的Android JDBC驱动程序”。

您是否尝试过查看ContentProviders? http://developer.android.com/guide/topics/providers/content-providers.html

通过实现(例如SQLite数据库)而不是直接访问MySQL,您可能会更幸运地访问数据。

要在android中提供与远程数据库的连接,将需要使用WebService,因为android没有连接到远程数据库的API。