MS Access is crap!

All right, I'm sick of this piece crap! Is MS a piece-o-crap or am I coding this wrong?!? Why am I getting "Invalid Cursor State" errors when the ResultSet reaches it's end? I get the data just fine, but then I get this stupid message! Needless to say, I'm pretty frustrated from wasting my time on this all day. Please help!

[349 byte] By [m_merrick] at [2007-9-26 3:26:46]
# 1
Let's see your code... are you checking for end of result set?while (myResultSet.next()) {// do your stuff here...}
ldadams at 2007-6-29 11:48:49 > top of Java-index,Archived Forums,Java Programming...
# 2

Here's a simplified version of what I trying. It's just basic DB operation... nothing fancy. That's why I don't understand why it's generating this error. The same code applied to Oracle doesn't produce this error. Is this somthing that i just have to deal with if using MS Access? Like I say, I'm getting the data, but it's a real annoyance to have to deal with this error.

Code:

import java.sql.*;

public class MSADB {

Connection cn = null;

Statement st = null;

ResultSet rs = null;

String sql = null;

int i = 0;

public MSADB(String dsn) {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(ClassNotFoundException cnfE) {

System.out.println(cnfE.getMessage());

}

// MS Access drivers generate "Invalid Cursor State" error when EOF condition

// occurs during rs.next(). The error is trapped by the catch() method, but any

// subsequent code must be placed following the catch() block.

//Attempt to access data

try {

cn = DriverManager.getConnection("jdbc:odbc:" + dsn);

st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY);

st.setFetchDirection(ResultSet.FETCH_UNKNOWN);

//sql = "SELECT count(*) FROM Calendars";

sql = "SELECT DelCal , NewCal FROM Calendars ORDER BY NewCal";

rs = st.executeQuery(sql);

while(rs.next()) {

i++;

System.out.println("Record " + i + "::\t" + rs.getString(1) + "\t" + rs.getString(2));

}

rs.close();

st.close();

cn.close();

}

//Catch SQL Exceptions

catch(SQLException sqlE) {

System.out.println(sqlE.getMessage());

}

}

public static void main(String[] args) {

String dsn = null;

if(args.length > 0) dsn = args[0];

else System.exit(0);

MSADB d = new MSADB(dsn);

}

}

m_merrick at 2007-6-29 11:48:49 > top of Java-index,Archived Forums,Java Programming...
# 3

I've worked on several projects using it and it has worked just fine.

Invalid cursor state errors usually happen where you are not positioned on a valid row in the Result Set.

I usually use;

Connection database;

//Your code for establishing a connection goes here

...

Statement s = database.createStatement();

ResultSet rs = s.executeQuery("Some SQL Query");

while(rs.next())

{

//Your Data Manipulation here

...

}

this assures me that the manipulation will only occur in valid rows of the ResultSet object

ojsuarez at 2007-6-29 11:48:49 > top of Java-index,Archived Forums,Java Programming...
# 4
Try to putrs.moveToCurrentRow();rs.beforeFirst(); before calling rs.next().This way you should guaranty to be on the valid row.
dsklyut at 2007-6-29 11:48:49 > top of Java-index,Archived Forums,Java Programming...