[英]How do I create a jdbc sqlite connection based with an ActionResolver?
I've followed the instructions at the wiki ( https://code.google.com/archive/p/libgdx-users/wikis/SQLite.wiki ) but I'm not sure how to actually use the db connection.我已经按照 wiki ( https://code.google.com/archive/p/libgdx-users/wikis/SQLite.wiki ) 上的说明进行操作,但我不确定如何实际使用 db 连接。 I've looked at the jdbc tutorial at http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm and I guess I'm stuck on the connection.我看过http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm上的 jdbc 教程,我想我卡在了连接上。
The resolver code解析器代码
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;
}
}
The jdbc tutorial has jdbc教程有
conn = DriverManager.getConnection(DB_URL,USER,PASS);
Can someone explain or point me in the right direction on how to use the connection from the ActionResolvers?有人可以解释或指出我如何使用 ActionResolvers 的连接的正确方向吗?
What do I need to do in order to have the conn available in the rest of my code?我需要做什么才能在我的其余代码中使用 conn?
I would think it would be something like我认为它会是这样的
Connection conn = ActionResolver.getConnection();
but that's not it.但不是这样。
After some trial and error and a lot of reading I have gotten this to work.经过一些试验和错误以及大量阅读后,我已经开始工作了。 Here is the code这是代码
MyGame.java我的游戏
public class MyGame extends Game {
public ActionResolver actionResolver;
public MyGame(ActionResolver actionResolver) {
super();
this.actionResolver = actionResolver;
}
public void create() {
Connection conn = actionResolver.getConnection();
try {
Statement statment = conn.createStatement();
ResultSet resultSet = statment.executeQuery("select * from items");
while (resultSet.next())
{
System.out.println(resultSet.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
AndroidLauncher.java change AndroidLauncher.java 更改
initialize(new MyGame(), config);
to到
initialize(new MyGame(new AndroidActionResolver(this.getBaseContext())), config);
DesktopLauncher.java change DesktopLauncher.java 更改
new LwjglApplication(new MyGame(), config);
to到
new LwjglApplication(new MyGame(new DesktopActionResolver()), config);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.