适用于 Android 的 SQLDroid JDBC 驱动程序
我需要在 Android 应用程序上使用数据库,因为使用时用户无法访问互联网。为此,我想通过 JDBC 使用 SQL Lite 连接到数据库。经过一番研究,我发现 Android API 不支持它,但有一个项目可以做到这一点: SQLDroid
我下载了 jar 并按照主要教程进行操作,但我不断收到 sql 异常 java.sql.SQLException:没有合适的驱动程序
当我想创建与 DriverManager 的连接时。
String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
Connection con = DriverManager.getConnection(url);
我做错了什么? 顺便说一句,我的活动名称为“AndroidActivity”,包名为 com.mypackage.droid。
编辑:完整代码:
public class AndroidActivity extends Activity {
String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
static Connection con;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Class.forName("SQLite.JDBCDriver");
con = DriverManager.getConnection(url);
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I need to use a database on an Android application, since when in use the user won't have internet access. For that, I want to do a connection to the database via JDBC with SQL Lite. After some research I found out that it is not supported by the Android API, but there is project which does just that: SQLDroid
I downloaded the jars and followed the main tutorial, but I keep getting an sql exception java.sql.SQLException: No suitable driver
when i want to create the connection with the DriverManager.
String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
Connection con = DriverManager.getConnection(url);
What am I doing wrong?
By the way, my activity has the name "AndroidActivity" and the package is called com.mypackage.droid.
Edit: Complete code:
public class AndroidActivity extends Activity {
String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
static Connection con;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Class.forName("SQLite.JDBCDriver");
con = DriverManager.getConnection(url);
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否声明过:
在声明连接之前
? 编辑:
抱歉,我以为您想使用本机 Android JDBC 驱动程序。在这种情况下,这就是我的方法。
但由于您使用的是 SQLDroid 库,它带来了自己的驱动程序,因此您必须首先注册驱动程序。喜欢:
Did you declare:
before declaring the connection?
EDIT:
Sorry I thought, that you want to use the native Android JDBC driver. In this case it would have been my approach.
But since you are using the SQLDroid library which brings an own driver you have to register the driver first by smth. like:
对于 sqldroid-1.0.0RC1.jar,我使用
有时驱动程序可以通过 drivermanager 加载,有时会失败。
作为一名工作人员,我直接使用 SQLDroidDriver,这一直有效。
With sqldroid-1.0.0RC1.jar I am using
Sometimes the driver can load via the drivermanager and sometimes it fails.
As a workaourd i am directly using SQLDroidDriver and this works all of the time.