开发者

How to scroll through mysql database in java

I'm trying to scroll over all the contents of a database. How do I properly do this:

conc conclass = new conc();
    开发者_Python百科    Connection conn = conclass.dbConnect();
        try{


       Statement statement=conn.createStatement();
        ResultSet rs;
        String sql;
        rs=statement.executeQuery("SELECT * FROM q_table");

        if(rs.next()){
        String yo=rs.getString("Question");
        jTextArea1.setText(yo);
        }





        }catch(Exception e){e.printStackTrace();}

And here's my class for connecting to the database:

 public Connection dbConnect() {
    try {
        String db_connect_string="jdbc:mysql://localhost:3306/questions";
        String db_userid="root";
        String db_password="1234";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);


        return conn;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

What is wrong with my code? It loads the first record when I click on the scroll button. But when I click it again. It doesn't do anything at all.


To collect all results from table (I assume you mean the table rather than the whole database):

while(rs.next()) {
   String yo=rs.getString("Question");
   // add 'yo' to a list or similar...
}

You need to iterate across your ResultSet. The ResultSet scrolls over each row returned from the database in sequence. See this example for more details.


You need to keep the Resultset open. For every click on the button, you must call rs.next() once. After that, a new row will copied into the ResultSet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜