-
databases
HI I was wondering if I could get some help here.
I have a java program that connects to an Access database. This is just a small simple program. The database table has a primary key and it is the first field with numbers in it going from 1 and up for each record. I would like to be able to goto the last record in the table and find out what the number is in the primary key. I used the last(); method and the program compiles fine but on runtime it gives me an error message -> Result set type is TYPE_FORWARD_ONLY 0 null
Can anyone help.
Here is my code:
import java.sql.*;
public class test {
public static void main(String[] arguments) {
String data = "jdbc dbc:tester1";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(
data, "", "");
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * FROM Table1 ");
rec.last();
System.out.println(rec.getInt(1));
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println(" Error: " + e.toString()
+ e.getMessage());
}
}
}
-
The problem is that you don't have a "scrollable" cursor which you need for last. By default most cursors are TYPE_FORWARD_ONLY.
You can request a scrollable cursor by using a different method for creating your statement.
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = statement.executeQuery(YOUR_SQL_GOES_HERE);
Consult the java.sql. API for more information on this.
-
If you want the highest value, then you could use something like:
"select max(colName) \"MAXPRIMARY\" from aTable"
if that works with MS Access....
eschew obfuscation
-
 Originally Posted by sjalle
If you want the highest value, then you could use something like:
"select max(colName) \"MAXPRIMARY\" from aTable"
if that works with MS Access....
Oh silly me. I only read the original problem and not what the poster was actually trying to do.
Just to confirm. Access supports the MAX function.
So "SELECT MAX(primarykeycolumnname) FROM Table1" will work.
Similar Threads
-
By ASP beginner in forum ASP.NET
Replies: 0
Last Post: 04-11-2005, 02:31 AM
-
By Hrayr Galoyan in forum Architecture and Design
Replies: 6
Last Post: 12-20-2002, 12:40 PM
-
By Aleksandr in forum VB Classic
Replies: 0
Last Post: 08-16-2001, 04:36 PM
-
By Bryan in forum VB Classic
Replies: 2
Last Post: 07-14-2000, 09:49 AM
-
By Bryan in forum VB Classic
Replies: 7
Last Post: 06-28-2000, 09:41 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks