0

Is there a method in which I can retrieve all the data from a particular database column and store it in a string array? My database currently looks like this:

public class DatabaseTotalEntries {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "appliance_name";
public static final String KEY_TOTAL_TIME = "appliance_running_time";
public static final String KEY_TOTAL_COST = "appliance_cost_usage";
public static final String KEY_TOTAL_ENERGY = "appliance_energy_usage";

private static final String DATABASE_NAME = "ApplianceTotaldb"; // The name of the database

private static final String DATABASE_TABLE = "AllEntries"; // The name of the database table

private static final int DATABASE_VERSION = 1;

and I am looking to create a method in which the data under the column 'KEY_TOTAL_COST' for every row/entry in the database is retrieved and stored in a String array

Any hep would be appreciated

4

2 回答 2

2

使用标准方法通过数据库的 JDBC 驱动程序从数据库中检索数据。创建准备好的语句,在创建连接时执行它并从结果集对象中提取数据。正如Mihai Todor的回答中提到的,在SQL查询中,只选择KEY_TOTAL_COST,如果你只查询你感兴趣的列会更快。当你从结果集中提取数据时,你可以把它放到你想要的任何数据结构中, 并根据需要对其进行操作(截断、拆分、转换为数字等...)

您可能会发现本教程很有用: http: //docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html ...或者只是尝试在 google 上查找更多“JDBC 结果集”使用示例。

于 2012-04-29T21:08:00.123 回答
1

那么,您需要使用的 SQL 语句是这样的:select appliance_running_time from ApplianceTotaldb.AllEntries. 为了运行它,我想这取决于您使用的 RDBMS。是 MySQL 吗?尝试在 Google 上搜索 JDBC 教程...

于 2012-04-29T20:59:23.290 回答