使用ORMlite从任何表获取数据的通用方法

时间:2015-03-09 10:46:11

标签: java database sqlite dao

我正在尝试使用ormlite和Dao类创建可以从SQLite中获取所有数据的方法,我不知道在Dao中设置了哪些值,代码如下:

public <T> List<T> getAllRecords(Class<T> klass){
    String databaseUrl = "jdbc:sqlite:c:\\Users\\workspace\\my_sqlite";
    SQLiteDBLayer sQLiteDBLayer = new SQLiteDBLayer(SQLiteDBLayer.ConnectionType.READER);
    ConnectionSource connectionSource = null;
    try {
        connectionSource = new JdbcConnectionSource(databaseUrl);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Dao<???, String> transfor = null ; //???? is where I'm stuck
    List<T> results;
    try {
        transfor =DaoManager.createDao(connectionSource,????);//And Here also
        results= (List<T>) transfor.queryForAll();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了它:

 Dao<T, String> transfor = null ; //solve 
    List<T> results;
    try {
        transfor =DaoManager.createDao(connectionSource,klass);//Solve
        results= (List<T>) transfor.queryForAll();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }