(multiple) ResultSet problem

I'm quite new to Java, but I couldn't find the answer to my question anywhere.

1. It's an applet

2. I'm using a MS Access 2000 ODBC/JDBC database

3. Everything seems to work. I can connect, I can see the database entry if using the first ResultSet ("SELECT * FROM PARKEERGARAGE WHERE DATUM ='" + datum + "';"). But my 2nd resultset doesn't work. ( else { if (x == 5 || x == 6 || x == 7 || x == 1 || x == 2) { ......... )

4. This is only a small part of a huge applet.

This is what I want:

1. Check if the string is in the database (if yes, show it... that works)

2. If it isn't, calculate (using GregorianCalendar) if it it is a Week or Weekend. (this doesn't work)

3. Whatever it is, retrieve the row with Date = Week or Weekend.

public void zoekOpeningstijden() {

GregorianCalendar calendar;

calendar = new GregorianCalendar(2004,12,30);

}

int i = 0;

String datum = beh_textfield_openingstijden_datum.getText();

int x = calendar.get(calendar.DAY_OF_WEEK);

connect();

try {

ResultSet rs = dbDAS.executeQuery("SELECT * FROM PARKEERGARAGE WHERE DATUM ='" + datum + "';");

while (rs.next()) {

i++;

}

if (i == 1) {

rs.previous();

panel_beh_openingstijden_textarea.setText(rs.getString("DATUM"));

rs.close();

}

else {

if (x == 5 || x == 6 || x == 7 || x == 1 || x == 2) {

rs = dbDAS.executeQuery("SELECT * FROM PARKEERGARAGE WHERE DATUM = 'Week';");

System.out.println(x);

System.out.println(rs.getString("DATUM"));

panel_beh_openingstijden_textarea.setText(rs.getString("DATUM"));

rs.close();

} else if (x == 3 || x == 4) {

rs = dbDAS.executeQuery("SELECT * FROM PARKEERGARAGE WHERE DATUM = 'Weekend';");

panel_beh_openingstijden_textarea.setText(rs.getString("DATUM"));

rs.close();

}

}

} catch (IOException ex) {} catch (SQLException ex) {}

close();

}

[2030 byte] By [SWATa] at [2007-10-1 1:00:04]
# 1
Try doing something with the exceptions. Maybe print them} catch (IOException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); }
bbrittaa at 2007-7-8 1:19:13 > top of Java-index,Security,Event Handling...
# 2
DONT "close" it unless you're done with your querying.....
angeles1016a at 2007-7-8 1:19:13 > top of Java-index,Security,Event Handling...
# 3

I added the close() statement, because I thought it might help, guess not.

catch (IOException ex) {System.out.println("IO ex:" + ex.toString());} catch (SQLException ex) {System.out.println("SQL ex:" + ex.toString());}

This returns nothing (so no errors here)

System.out.println(rs.getString("DATUM"));

This returns nothing (not even a print line)

System.out.println(rs);

This returnssun.jdbc.odbc.JdbcOdbcResultSet@16c79d7

SWATa at 2007-7-8 1:19:13 > top of Java-index,Security,Event Handling...
# 4

You are not correctly using the ResultSet.

1) In the case where i ==1, calling rs.previous() returns the cursor before the first record. Remove the rs.previous() call.

2) The general use of a ResultSet is:

//...

ResultSet rs = connection.executeXXXX("SELECT ...");

while (rs.next()) {

// At each iteration of the loop, the cursor is positionned on the next record

// so, here you will read the fields and do your stuff with them

// For example:

String productCode = rs.getString(1); // first field of current record

int productQty = rs.getInt(2); // second field of current record

double productPrice = rs.getDouble(3); // third field of current record

//...

}

3) Concerning the date problem, read more carefully the API Javadoc for the GregorianCalendar constructor:

public GregorianCalendar(int year, int month, int date)

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

Parameters:

year - the value used to set the YEAR time field in the calendar.

month - the value used to set the MONTH time field in the calendar. Month value is 0-based. e.g., 0 for January.

date - the value used to set the DATE time field in the calendar.

So, for DECEMBER you should use 11 instead of 12.

Hope this helped,

regards.

Franck_Lefevrea at 2007-7-8 1:19:13 > top of Java-index,Security,Event Handling...