Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将数据库表的特定行值获取到arraylist中_Sql_Sqlite - Fatal编程技术网

Sql 将数据库表的特定行值获取到arraylist中

Sql 将数据库表的特定行值获取到arraylist中,sql,sqlite,Sql,Sqlite,我正在尝试将某些行记录从db_表中获取到数组列表中。虽然这已经做了很多次了,但我的有点不同。 假设我的数据库中有一个如下所示的表: +---------+--------+--------+ | NAME | BILL | CONTACT| +---------+--------+--------+ | james | 400 | 024669 | | Mavis | 700 | 025550 |

我正在尝试将某些行记录从db_表中获取到数组列表中。虽然这已经做了很多次了,但我的有点不同。 假设我的数据库中有一个如下所示的表:

    +---------+--------+--------+
    |  NAME   | BILL   | CONTACT|
    +---------+--------+--------+
    |  james  |   400  | 024669 |            
    |  Mavis  |   700  | 025550 |              
    |  john   |   650  | 029510 |         
    |  bendo  |   340  | 023579 |
    +---------+--------+--------+
我想要一个数组来显示这样的记录:

[james,400,024669]
[Mavis,700, 025550]
[John,650, 029510]
[bendoo,340,023579] 
请问我如何做到这一点。我编写的代码只提供了最后一个数组。及其:

    ArrayList<String> inner = null;

    try{
    String sql="select Name,Score,Contact from members";
    pst=conn.prepareStatement(sql);
    rs=pst.executeQuery();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnsNumber = rsmd.getColumnCount();
    while (rs.next()) {
    inner = new ArrayList<>(); 
    for(int i=1; i<=columnsNumber; i++){
    inner.add(rs.getString(i));
    }    

}
        System.out.println(inner);
    }catch(Exception e){
}
请告诉我如何获得以下结果:

[james,400,024669]
[Mavis,700, 025550]
[John,650, 029510]
[bendoo,340,023579] 

将“inner=new ArrayList();”移到“while”语句之前。您正在为每一行重新初始化arraylist。

在“while”语句之前移动“inner=new arraylist();”。您正在为每一行重新初始化arraylist。

您需要将
内部
添加到全局列表中。否则,每次都会丢失行值,而只保留最后一个

您的代码应更改为以下内容:

ArrayList<String> inner = null;

try {
  String sql = "select Name, Score, Contact from members";
  pst = conn.prepareStatement(sql);
  rs = pst.executeQuery();
  List<List<String>> allRows = new ArrayList<List<String>>(); // add this!

  while (rs.next()) {
    inner = new ArrayList<>(); 
    for(int i=1; i<=columnsNumber; i++){
      inner.add(rs.getString(i));
    }
    System.out.println("ROW: "+row); // Add this line.
    allRows.add(inner); // add this line!
  }   

  for (List<String> row : allRows) {
    System.out.println(row);
  }
} catch (SQLException e) {
  e.printStackTrace();
}
ArrayList-inner=null;
试一试{
String sql=“从成员中选择姓名、分数、联系人”;
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
List allRows=new ArrayList();//添加这个!
while(rs.next()){
内部=新的ArrayList();

对于(int i=1;i您需要将
internal
添加到全局列表中。否则每次都会丢失行值,只保留最后一个

您的代码应更改为以下内容:

ArrayList<String> inner = null;

try {
  String sql = "select Name, Score, Contact from members";
  pst = conn.prepareStatement(sql);
  rs = pst.executeQuery();
  List<List<String>> allRows = new ArrayList<List<String>>(); // add this!

  while (rs.next()) {
    inner = new ArrayList<>(); 
    for(int i=1; i<=columnsNumber; i++){
      inner.add(rs.getString(i));
    }
    System.out.println("ROW: "+row); // Add this line.
    allRows.add(inner); // add this line!
  }   

  for (List<String> row : allRows) {
    System.out.println(row);
  }
} catch (SQLException e) {
  e.printStackTrace();
}
ArrayList-inner=null;
试一试{
String sql=“从成员中选择姓名、分数、联系人”;
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
List allRows=new ArrayList();//添加这个!
while(rs.next()){
内部=新的ArrayList();

对于(inti=1;iI添加了一个额外的System.out.println()。你现在看到所有四行显示了吗?如果没有,那么你的数据库数据是坏的。我看到了,兄弟。谢谢你,但我需要额外的帮助。我正在将此与sms api一起使用以发送个性化消息。不幸的是,它们数组“allRows”似乎没有被sms“send method”识别。将显示样本确定我已解决它。非常感谢uch@The_Impaler。只是我这边的一个小错误。两个试抓块导致了这个问题。再次感谢大家。againI添加了一个额外的系统。out.println()。你现在看到所有四行显示了吗?如果没有,那么你的数据库数据是坏的。我看到了,兄弟。谢谢你,但我需要额外的帮助。我正在将此与sms api一起使用以发送个性化消息。不幸的是,它们数组“allRows”似乎没有被sms“send method”识别。将显示样本确定我已解决它。非常感谢uch@The_Impaler.只是我这边的一个小错误。两个试块和接住块造成了问题。再次感谢大家
 ArrayList<String> inner = null;

try {
  String sql = "select Name,Score, Contact from members";
  pst = conn.prepareStatement(sql);
  rs = pst.executeQuery();
  ResultSetMetaData rsmd = rs.getMetaData();
  int columnsNumber = rsmd.getColumnCount();
  List<List<String>> allRows = new ArrayList<List<String>>(); // add this!

  while (rs.next()) {
    inner = new ArrayList<>(); 
    for(int i=1; i<=columnsNumber; i++){
      inner.add(rs.getString(i));
    }
    allRows.add(inner); // add this line!
  }   

  for (List<String> row : allRows) {
    System.out.println(row);
  }
} catch (SQLException e) {
}
    try{
    // initialise SMS object. 
    ZenophSMS sms = new ZenophSMS(); 
    sms.setUser("example@gmail.com"); 
    sms.setPassword("xxxxxxx");
    sms.authenticate();
    // set message parameters.
    sms.setMessage("Hello {$name}, your score is  {$score} . please come for you money ");  
    sms.setSenderId("NICE ONE"); 
    sms.setMessageType(MSGTYPE.TEXT);

    // add destinations. 
    for ( ArrayList<String> recpt : allRows) sms.addRecipient(recpt.get(2), new String[] {recpt.get(0), recpt.get(1)}, false);

    // submit the message. Since the total number of destinations is 
    // less than 400, the submit status of the destinations will be returned. 
    List<String[]> resp = sms.submit();

    // show the status 
    for (String[] dest : resp) 
    { REQSTATUS st = REQSTATUS.fromInt(Integer.parseInt(dest[0]));
    if (null != st) 
        switch (st) {
            case SUCCESS:
             System.out.println("Submitted: %s"+ dest[1]);
                break;
            case ERR_INSUFF_CREDIT:
               System.out.println("Insufficient Credits: %s"+dest[1]);
                break;
            default:
             System.out.println("Failed: %s"+ dest[1]);
                break;
        }
}

    }
catch (SMSException ex) { System.out.println("Error: " + ex.getMessage()); }
catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); }
 for ( ArrayList<String> recpt : allRows) sms.addRecipient(recpt.get(2),new String[] {recpt.get(0), recpt.get(1)}, false);