简体   繁体   English

如何创建基于 ActionResolver 的 jdbc sqlite 连接?

[英]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.

相关问题 如何使用sql2o使用createQuery()方法创建JDBC连接? - How do I use createQuery() Method to create JDBC Connection, using sql2o? 当 jdbc 与 sqlite3 数据库连接时,我该怎么做才能避免“内存不足”的错误? - What do I have to do to avoid error of “out of memory”, when connection by jdbc with sqlite3 database? 如何使用 JDBC 和连接池实现 DAO 管理器? - How do I implement a DAO manager using JDBC and connection pools? 为什么我需要使用sqlite java的jdbc? - why do i need jdbc with sqlite java? 如何减轻连接com.mysql.jdbc.JDBC4Connection@11d08960触发的连接泄漏, - How do I mitigate Connection leak triggered for connection com.mysql.jdbc.JDBC4Connection@11d08960, 如何在hibernate 4.3 Integrator中获取JDBC连接? - How do I fetch a JDBC connection in a hibernate 4.3 Integrator? 如何使用JDBC创建表并将INSERT记录插入表中? - How do I CREATE a table and INSERT records into table using JDBC? 如果使用SQLite JDBC,连接速度慢 - Slow connection in case of SQLite JDBC 如何与sqlserver2000建立jdbc连接? - how to do jdbc connection with sqlserver2000? 如何使用字符串变量通过Java的jdbc从SQLite数据库中检索行? - How do I retrieve a row from a SQLite database through Java's jdbc using a string variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM