JAVA: How to access DB multiple times using a JButton

Hello, and I’m new to JDBC and SQL. My question is how do you access database twice for a given SQL query? I’m working on a small project which stores data in H2 DB and retrieve it from different ways. There are 2 buttons and if you click 1 it displays the data in JTable with the query. It shows the data from DB when I click a button to call any 1 of this first then the 2nd method won’t.
My problem is these 2 methods throw JDBC.Exception: No data available [2000-193] if I call 1 method first then the another

 "SELECT * FROM table_name WHERE expirationDate > now() " 

and the other 1 displays expiredDate data in JTable using the query

"SELECT * FROM table_name WHERE expirationDate < now()" 

Here are my methods

[code]
public void readActiveData() throws IOException, InstantiationException, IllegalAccessException, SQLException, ClassNotFoundException {

try {  
  Class.forName("org.h2.Driver");
  conn = DriverManager.getConnection(URL,USER,PASSWORD);
  PreparedStatement pst = null;
  ResultSet rs;
  Statement st = conn.createStatement();
            try {
	             rs = st.executeQuery("SELECT * FROM data WHERE expirationDate > NOW();");	
                 rs.beforeFirst();
 
                  while(rs.next()) {
	                                            int id = rs.getInt(1);
	                                            String fullName = rs.getString(2);
	                                            String regNum = rs.getString(3);
	                                            String note = rs.getString(4);
                                                java.sql.Date currentDate = rs.getDate(5);	
                                                int totalPay = rs.getInt(6);
                                                expirationDate = rs.getDate(7);
	                                            rows++; }
                    regData = new Object[rows][7];
                    rs = st.executeQuery("SELECT * FROM data WHERE expirationDate > NOW();");
                      
                       for (int i1 = 0; i1 < rows; i1++)
			{ rs.next();
	           for(int j1 = 0; j1 < 7; j1++) {
			regData[i1][j1]=rs.getString(j1+1); }
	                                     }
                                 model = new DefaultTableModel(regData, colHeads);
                                 table.setModel(model);
                                 JScrollPane jsp = new JScrollPane(table,v,h);
	                     activeDataPanel.add(jsp);
                                 rs.close();
                                 st.close();
                                 conn.close();  }
                  catch(Exception e) {
                                           JOptionPane.showMessageDialog(null,"Error: " + e); }
                                                }
                 catch (ClassNotFoundException | SQLException ex){
                                           JOptionPane.showMessageDialog(null,"Error: " + ex);
                                               }
}[/code]

and the other method is pretty much similar but just the SQL queries are different

[code]
public void readExpiredData() throws IOException, InstantiationException, IllegalAccessException, SQLException, ClassNotFoundException {

try {  
  Class.forName("org.h2.Driver");
  conn = DriverManager.getConnection(URL,USER,PASSWORD);
  PreparedStatement pst = null;
  ResultSet rs;
  Statement st = conn.createStatement();
            try {
	             rs = st.executeQuery("SELECT * FROM data WHERE expirationDate > NOW();");	
                 rs.beforeFirst();
 
                  while(rs.next()) {
	                                            int id = rs.getInt(1);
	                                            String fullName = rs.getString(2);
	                                            String regNum = rs.getString(3);
	                                            String note = rs.getString(4);
                                                java.sql.Date current = rs.getDate(5);	
                                                int totalPay = rs.getInt(6);
                                                expirationDate = rs.getDate(7);
	                                            rows++; }
                    regData = new Object[rows][7];
                    rs = st.executeQuery("SELECT * FROM data WHERE expirationDate < NOW();");
                      
                       for (int i1 = 0; i1 < rows; i1++)
			{ rs.next();
	           for(int j1 = 0; j1 < 7; j1++) {
			regData[i1][j1]=rs.getString(j1+1); }
	                                     }
                                 model = new DefaultTableModel(regData, colHeads);
                                 table.setModel(model);
                                 JScrollPane jsp = new JScrollPane(table,v,h);
	                     activeDataPanel.add(jsp);
                                 rs.close();
                                 st.close();
                                 conn.close();  }
                  catch(Exception e) {
                                           JOptionPane.showMessageDialog(null,"Error: " + e); }
                                                }
                 catch (ClassNotFoundException | SQLException ex){
                                           JOptionPane.showMessageDialog(null,"Error: " + ex);
                                               }
}[/code]

Any suggestions how i can let this work?

Do you want to convert the Java code to Xojo?

MBS Plugins come with Java Plugin.
So you can call java classes from Xojo including the use of JDBC database drivers.

See
http://www.monkeybreadsoftware.de/xojo/plugin-java.shtml

And
http://www.monkeybreadsoftware.net/plugins-mbsjavaplugin.shtml

What is with people using different languages coming to this Xojo forum for help lately?
We use the Xojo programming language, not Java, C, C++, C#…
I don’t get it.
Did we get posted as a help forum somewhere?

Next it will be “Hey I want to build a garden shed and I can quite figure out how to …”

Anyone know anything about cleaning the carb in a Craftsman 24.5 HP riding mower ? :stuck_out_tongue:

Step 1, melt the snow

You have to melt snow to fix a engine carb? Who knew

[quote=325863:@Norman Palardy]Next it will be “Hey I want to build a garden shed and I can quite figure out how to …”

Anyone know anything about cleaning the carb in a Craftsman 24.5 HP riding mower ? :P[/quote]

Perfect timing Norm, I need to build a shed so I can work on my Craftsman 24.5 HP riding mower :stuck_out_tongue:

Warmest spring since 1880 here. I’m more familiar with rubble walls than with garden sheds.

I suspect that “we” as a group are being recognized as a place to get help from friendly folks rather than those sarcastic ones elsewhere. We should either help them with Xojo code or gently move them back to the dark side.

Ridicule however does not make friends.

And just to make my point absolutely clear I “mow” my lawn with a weedwacker, this whole thing with a 24.5 HP ride on just doesn’t work for me:)