【问题标题】:How do I create a jdbc sqlite connection based with an ActionResolver?如何使用 ActionResolver 创建 jdbc sqlite 连接?
【发布时间】: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


【解决方案1】:

经过反复试验和大量阅读后,我已经完成了这项工作。这是代码

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 改变

initialize(new MyGame(), config);

initialize(new MyGame(new AndroidActionResolver(this.getBaseContext())), config);

桌面启动器.java 改变

new LwjglApplication(new MyGame(), config);

new LwjglApplication(new MyGame(new DesktopActionResolver()), config);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多