1

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

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

我怎样才能做到这一点..??这就是我在 android 应用程序中使用的代码,它获取数据库的结果但不能转换为光标:

    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

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

谢谢

4

2 回答 2

1

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

于 2013-10-09T08:18:45.007 回答
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);
        .....
    }

希望这可以帮助。

于 2019-04-01T10:58:43.587 回答