SQLDroid JDBC驱动程序为Android [英] SQLDroid JDBC driver for Android
问题描述
我需要在一个Android应用程序使用的数据库,因为在使用时,用户将无法上网。对于这一点,我想这样做的数据库通过JDBC和SQL精简版的连接。经过一番研究,我发现它不支持Android的API,但是有项目,它做到了这一点:的 SQLDroid
我下载了罐子,随后主教程,但我不断收到SQL异常值java.sql.SQLException:没有合适的驱动程序
时,我想创建一个类DriverManager的连接。
字符串URL =的jdbc:sqldroid:+/data/data/com.mypackage.droid+/main.sqlite;
连接CON =的DriverManager.getConnection(URL);
我是什么做错了吗? 顺便说一句,我的活动的名称为AndroidActivity和包被称为com.mypackage.droid。
编辑:完成code:
公共类AndroidActivity延伸活动{
字符串URL =的jdbc:sqldroid:+/data/data/com.mypackage.droid+/main.sqlite;
静态连接CON;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
尝试 {
的Class.forName(SQLite.JDBCDriver);
CON =的DriverManager.getConnection(URL);
}赶上(java.sql.SQLException中的E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(ClassNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
你声明:
的Class.forName(SQLite.JDBCDriver);
在声明连接?
编辑:
对不起,我以为,要使用原生的Android JDBC驱动程序。在这种情况下,这本来是我的做法。
但是,由于您使用的是SQLDroid库,它带来了自己的驱动程序,你必须通过未便先注册的驱动程序。这样的:
的Class.forName(org.sqldroid.SqldroidDriver)的newInstance()。
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();
}
}
Did you declare:
Class.forName("SQLite.JDBCDriver");
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:
Class.forName("org.sqldroid.SqldroidDriver").newInstance();
这篇关于SQLDroid JDBC驱动程序为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!