I’m trying to scroll over all the contents of a database. How do I properly do this:
conc conclass = new conc();
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.
You need to keep the
Resultsetopen. For every click on the button, you must callrs.next()once. After that, a new row will copied into theResultSet.