简体   繁体   English

将MySQL表中的行存储到Java中的数组中

[英]Storing rows from a MySQL table into an array in Java

I'm trying to store rows from a table into an array. 我正在尝试将表中的行存储到数组中。 I can get the first result and store that but I cannot seem to be able to store any of the other data. 我可以获得第一个结果并将其存储,但似乎无法存储任何其他数据。

This is the code I've written 这是我写的代码

try 
    {
        test = "select * from Arsenal order by 'MatchNumber' ASC";                
        rs = st.executeQuery(test); 

        while (rs.next()) 
        {
            //This retrieves each row of Arsenal table and adds it to an array in the Team Results class.

            matchno = rs.getString("MatchNumber");                    
            hometeam = rs.getString("HomeTeam");                      
            awayteam = rs.getString("AwayTeam");                         
            homegoals = rs.getString("HomeGoals");                   
            awaygoals = rs.getString("AwayGoals");                   
            result = rs.getString("Result");                             


            teams = (matchno + "," + hometeam + "," + awayteam + "," + homegoals + "," + awaygoals + "," + result);     // Takes all the variables containging a single customers information and puts it into a string, seperated by commas.
            TeamResults.add(matchno,hometeam,awayteam,homegoals,awaygoals,result);
        }
    }

Any idea where I'm going wrong? 知道我哪里出错了吗?

while -condition更改为hasNext()并在循环内使用next()将数据库光标向前移动。

Try to use this method bellow : 尝试使用以下方法:

 public void SelectData(String sqlcounter ,String sql){

        try {
         RsCounter=stmt.executeQuery(sqlcounter);
            System.out.println(sqlcounter);
            while(RsCounter.next()){

                countrow=RsCounter.getInt("COUNTR");
                System.out.println(countrow+"\n");
                             }
            System.out.println(sql);
         RsSelecting = stmt.executeQuery(sql);
         data=new String[countrow][RsSelecting.getMetaData().getColumnCount()];

         header= new String[RsSelecting.getMetaData().getColumnCount()];

     i=0;
     while(RsSelecting.next()){
      for(j=0;j<RsSelecting.getMetaData().getColumnCount();j++){

              data[i][j]=(RsSelecting.getString(j+1));
              header[j]=RsSelecting.getMetaData().getColumnName(j+1);
              System.out.print(data[i][j]+"\n");

     }    


      i++;

     }
      i=j=0;
        } catch (SQLException ex) {
                       ex.printStackTrace();

            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

where 哪里

sqlcounter ="select COUNT(*) as COUNTR from Arsenal order by 'MatchNumber' ASC";

and

sql ="select * from Arsenal order by 'MatchNumber' ASC";

Verify the column names once. 一次验证列名称。 Sometimes ALIAS doesn't work out, I am not sure why. 有时ALIAS问题,我不确定为什么。

Get the meta data from the result set: 从结果集中获取元数据:

ResultSetMetaData metaData = resultSet.getMetaData();
int size = metaData.getColumnCount();

for (int i = 0; i < size; i ++)
    System.out.println(metaData.getColumnName(i);

Also just for performance, list out the column names instead of using * in the SELECT query. 同样为了提高性能,请列出列名,而不要在SELECT查询中使用* Also, you can take a look at com.sun.rowset.CachedRowSetImpl. 另外,您可以看看com.sun.rowset.CachedRowSetImpl. It's used like: 它的用法如下:

CachedRowSetImpl crs = new CachedRowSetImpl();
crs.populate(resultSet);

I think it also implements CachedRowSet , but I am not entirely sure. 我认为它也实现了CachedRowSet ,但是我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM