【问题标题】:ResultSet to Android Cursor结果集到 Android 光标
【发布时间】:2013-10-09 07:16:11
【问题描述】:

我从我的 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

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

谢谢

【问题讨论】:

    标签: android jdbc cursor


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      戴夫说的是对的。我的数据库项目是在 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);
              .....
          }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多