Android中的JDBC连接 [英] JDBC connection in Android

查看:23
本文介绍了Android中的JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人在android中尝试过JDBC连接,因为在Android 2.3中支持JDBC.

Is there anyone who tried JDBC connection in android because in Android 2.3 JDBC is supported.

我必须在没有 Web 服务的情况下连接 Mysql.

I have to connect with Mysql without web service.

我已经申请了,但它给了我错误

I have made application but it gives me error

public class MysqlConnect extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "root";
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();
}
}
}

我在 getConnection 中遇到错误.错误就像 java.lang.VerifyError : com.mysql.jdbc.MysqlIO

i am getting error in getConnection. error is like java.lang.VerifyError : com.mysql.jdbc.MysqlIO

提前致谢.

推荐答案

我已经使用 JDBC 和用于各个数据库的桌面 Java 应用程序的常规类型 4 驱动程序直接从 Android 设备成功连接到 MySQL、Oracle 和 SQL Server.

I have successfully connected to MySQL, Oracle and SQL Server directly from an Android device using JDBC and the regular type 4 drivers used for desktop Java applications for the respective databases.

只需搜索桌面 JDBC 示例即可.相同的代码无需任何修改即可在 Android 上运行.确保将驱动程序的 .jar 文件添加到 Eclipse 中的项目中.编译器会自动将 .jar 驱动程序转换为 Dalvik 兼容包.

Just search for a desktop JDBC sample. The same code works without any modifications on Android. Make sure you add the .jar file of the driver to the project in Eclipse. The compiler will automatically convert the .jar driver into Dalvik compatible package.

这篇关于Android中的JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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