ResultSet到Android游标

时间:2013-10-09 07:16:12

标签: android jdbc cursor

我从Android应用程序连接到PC上的MySQL数据库。

我正在使用java.sql.jdb。现在我希望我的结果集进入android.database.cursor ??

我该怎么做.. ?? 这是我在Android应用程序中使用的代码,它获取数据库的结果,但无法转换为Cursor:

    Connection connect = null;
    ResultSet resultSet = null;
    Statement statement = null;

    try {
        connect = DriverManager.getConnection("jdbc:mysql://"+DbHelper.DB_Path+"/"+DbHelper.DB_Name+"?"
                + "user="+ DbHelper.DB_UserName+ "&password="+ DbHelper.DB_Pass);


        statement = connect.createStatement();
        // Result set get the result of the SQL query
        resultSet = statement
                .executeQuery("Select * from btag_store "+
                        "Where "+
                        "guid='"+filterArgs+"'");


        }
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Cursor cc;
    cc = (Cursor) resultSet; // error in type casr

我知道类型转换会给我错误,但还有其他方法吗?? ??

由于

2 个答案:

答案 0 :(得分:1)

简单地说,你不能。除非您愿意完成所有工作以定义实现Cursor接口的对象并使用ResultSet来实现Cursor的实现细节。但是,这有些愚蠢,因为ResultSet对象已经设计为迭代从数据库返回的结果。最干净的方法是按预期使用ResultSet对象。

答案 1 :(得分:0)

戴夫所说的是正确的。我的数据库项是基于Cursor(Sqlite)构建的,但是我需要与MySQL相同的入口点。所以我尝试了这个:

我创建了一个基类 AbstractCursorGen.java

import android.database.Cursor;

import java.sql.ResultSet;

public abstract class AbstractCursorGen {
    protected Cursor c;
    protected ResultSet rs;
    public abstract int getColumnIndex(String iName);
    public abstract String getString(String iName);
    public abstract int getInt(String iName);
    public abstract long getLong(String iName);
    public abstract boolean moveToNext();
    public abstract void close();
}

然后使用Cursor的那个将保存cursor的实例。通过直接将结果提供给列字符串,还有一个额外的好处。我的代码将此用于SQLite。

CursonGen.Java

import android.database.Cursor;

public class CursorGen extends AbstractCursorGen{
    public CursorGen(Cursor c)
    {
        this.c = c;
    }

    public int getColumnIndex(String iName)
    {
        return c.getColumnIndex(iName);
    }


    public String getString(String iName){
        return c.getString(getColumnIndex(iName));
    }

    public int getInt(String iName){
        return c.getInt(getColumnIndex(iName));
    }
    public long getLong(String iName){
        return c.getLong(getColumnIndex(iName));
    }

    public boolean moveToNext()
    {
        return c.moveToNext();
    }

    public void close()
    {
        c.close();
    }
}

然后在结果集上建立一个。这用于MySQL结果

ResultSetGen.java

import android.util.Log;

import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultSetGen extends AbstractCursorGen{
    public ResultSetGen(ResultSet rs)
    {
        this.rs = rs;
    }

    public int getColumnIndex(String iName)
    {
        try {
            return rs.findColumn(iName);
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
            return -1;
        }
    }


    public String getString(String iName){
        try {
            return rs.getString(getColumnIndex(iName));
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
            return null;
        }
    }

    public int getInt(String iName){
        try {
            return rs.getInt(getColumnIndex(iName));
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
            return -1;
        }
    }
    public long getLong(String iName){
        try {
            return rs.getLong(getColumnIndex(iName));
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
            return -1;
        }
    }

    public boolean moveToNext()
    {
        try {
            return rs.next();
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
            return false;
        }
    }

    public void close()
    {
        try {
            rs.close();
        } catch (SQLException ex)
        {
            Log.e("PROX","Column not found");
        }
    }
}

诀窍是仅为我实际使用的方法提供实现。

这最终由(一个示例)调用

public Person(AbstractCursorGen cursor)
    {
       setFromCursor(cursor);
    }

    protected void setFromCursor(AbstractCursorGen cursor)
    {
        PersonID        = cursor.getLong    (   COLUMN_PERSON_ID);
        ClusterID       = cursor.getInt     (   COLUMN_CLUSTER_ID);
        Name            = cursor.getString  (   COLUMN_NAME);
        .....
    }

希望这会有所帮助。