【发布时间】:2016-07-10 08:13:25
【问题描述】:
我已按照 wiki (https://code.google.com/archive/p/libgdx-users/wikis/SQLite.wiki) 上的说明进行操作,但我不确定如何实际使用 db 连接。我查看了http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm 的 jdbc 教程,我想我被卡在了连接上。
解析器代码
public interface ActionResolver {
public Connection getConnection();
}
public class DesktopActionResolver implements ActionResolver {
public Connection getConnection() {
String url = "jdbc:sqlite:db.sqlite";
try {
Class.forName("org.sqlite.JDBC");
return DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public class AndroidActionResolver implements ActionResolver {
Handler uiThread;
Context appContext;
public AndroidActionResolver(Context appContext) {
uiThread = new Handler();
this.appContext = appContext;
}
@Override
public Connection getConnection() {
String url = "jdbc:sqldroid:/data/data/com.myapp/databases/db.sqlite";
try {
Class.forName("org.sqldroid.SQLDroidDriver").newInstance();
return DriverManager.getConnection(url);
} catch (InstantiationException e) {
Log.e("sql", e.getMessage());
} catch (IllegalAccessException e) {
Log.e("sql", e.getMessage());
} catch (ClassNotFoundException e) {
Log.e("sql", e.getMessage());
} catch (SQLException e) {
Log.e("sql", e.getMessage());
}
return null;
}
}
jdbc教程有
conn = DriverManager.getConnection(DB_URL,USER,PASS);
有人可以解释或指出如何使用 ActionResolvers 的连接的正确方向吗?
我需要做什么才能让 conn 在我的其余代码中可用?
我觉得应该是这样的
Connection conn = ActionResolver.getConnection();
但事实并非如此。
【问题讨论】:
-
我不明白你的问题是什么。你有一些代码,它不起作用吗?如果是这样,它怎么行不通?
-
@mark-rotteveel
Connection conn = ActionResolver.getConnection();无效。这是我所期望的,但我想还有其他事情需要发生。我的问题是如何创建连接。
标签: java android sqlite jdbc libgdx