Java 安卓&x2B;使用com.MySQL.jdbc.Driver的MySQL
我正在编写一个Android应用程序,它将连接到MySQL服务器。目前,我正在使用XAMPP在我的计算机上测试MySQL服务器。下面的代码在严格作为JAVA应用程序进行测试时运行良好Java 安卓&x2B;使用com.MySQL.jdbc.Driver的MySQL,java,android,mysql,exception,inner-classes,Java,Android,Mysql,Exception,Inner Classes,我正在编写一个Android应用程序,它将连接到MySQL服务器。目前,我正在使用XAMPP在我的计算机上测试MySQL服务器。下面的代码在严格作为JAVA应用程序进行测试时运行良好 import java.sql.*; public class MySQL{ public static void main(String[] args) { System.out.println("MySQL Connect Example."); Connection conn = nul
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应用程序时,我得到了异常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.
我想这句话与我得到的例外情况有关。有没有人与MySQL和Android一起工作过,可以引导我走向正确的方向?
谢谢您尝试过查看内容提供商吗
与直接访问MySQL相比,通过该数据库的实现(例如SQLite数据库)访问数据可能更幸运。要在android中提供到远程数据库的连接,将需要使用WebService,因为android没有连接到远程数据库的API。使用JDBC连接器mysql-connector-java-3.0.17-ga-bin.jar,目前是唯一有效的
同时检查您的代码,您需要为应用程序的每个活动实现一个连接池,只需添加一个私有的“Connection”变量,并在OnCreate方法中初始化它,如con=DriveManager.getConnection。。。。。。然后每次需要访问MYSQL时都使用这个私有变量。另一种方法是使用一个使用三层架构的虚拟JDBC驱动程序:您的JDBC代码通过HTTP发送到一个远程Servlet,该Servlet在将其传递给MYSQL JDBC驱动程序之前过滤JDBC代码(配置和安全)。结果通过HTTP发送给您。有些自由软件使用这种技术。只需谷歌“Android JDBC驱动程序over HTTP”。你可能需要驱动程序的源代码,这样你就可以重新编译它。我需要其他手机上的其他用户可以使用这些数据。因此,内容提供者不是一个可接受的选择。