有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

尝试在Java应用程序中执行更多MySql查询时出现错误S1000

我在尝试对Java应用程序代码执行多个查询时遇到问题。 我有一个在main中调用的过程,它在类“Fant”中:

    public void XXX(){
     Connectivity con=new Connectivity(); // this class set up the data for the connection to db; if ( !con.connect() ) {
                    System.out.println("Error during connection.");
                    System.out.println( con.getError() );
                    System.exit(0);
                }
        ArrayList<User> blabla=new ArrayList<User>();
        blabla=this.getAllUsers(con);
        for (User u:blabla)
         {
           try {
                Connectivity coni=new Connectivity();//start a new connection each time that i perform a query
                Statement t;
                t = coni.getDb().createStatement();

               String query = "Select count(*) as rowcount from berebe.baraba";
               ResultSet rs = t.executeQuery(query);
               int numPrenotazioni=rs.getInt("rowcount");
                rs.close();                           //close  resultset
                t.close();                            //close statement
                coni.getDb().close();                  //close connection
                }
          }
            catch (SQLException e)
                 {

                  System.err.println("SQLState: " +
                  ((SQLException)e).getSQLState());
                  System.err.println("Error Code: " +
                  ((SQLException)e).getErrorCode());
                }
         }
            }

被调用函数的定义如下:

ArrayList<User> getAllUsers(Connectivity con) {
 try{
            ArrayList<User> userArrayList=new ArrayList<User>();
            String query = "Select idUser,bubu,lala,sisi,gogo,gg  from berebe.sasasa";
            Statement t;
            t = con.getDb().createStatement();
            ResultSet rs = t.executeQuery(query);

            while (rs.next())
            {
                User utente=new User(....); //user fields got from query
                userArrayList.add(utente);
            }
              rs.close();
              t.close();
              con.disconnect(); //disconnect the connection
              return userArrayList;
            } catch (SQLException e) {

        }
        return null;

    }

主要问题是:

 public static void main(String[] argv) {
    ArrayList<User> users=new ArrayList<User>();
    System.out.println("-------- MySQL JDBC Connection Testing ------------");
    Fant style = new Fant();
    style.XXX();

}

执行对“getAllusers”执行的查询,在arraylist“blabla”中有几个用户;问题是,需要计数的第二个查询永远不会执行。 运行时给出的MYSQlState为=“S1000”,SQLERROR为“0”。 可能我在连接问题上弄错了,但我不熟悉语句、连接和结果集。 谢谢


共 (1) 个答案

  1. # 1 楼答案

    XXX()方法中获取结果之前,您可能会忘记调用rs.next(),如下所示:

    ResultSet rs = t.executeQuery(query);
    
    // call rs.next() first here
    int numPrenotazioni=rs.getInt("rowcount");