This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes Insert and MoveFirst Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Insert and MoveFirst" Watch "Insert and MoveFirst" New topic
Author

Insert and MoveFirst

Minh Phan
Greenhorn

Joined: Sep 23, 2004
Posts: 8
After I inserted a record to Access database and call first to move to the first record the following error message is displayed: java.sql.SQLException: ResultSet is closed. Here is the code. Please help.

import java.io.*;
import java.sql.*;

class MyInsert {
static private Statement st;
static private Connection con;
static private ResultSet rs;

public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection(
"jdbc dbc:library","","");


st = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);

rs = st.executeQuery("select bkCode,bkName,bkAuthor,bkAvailableQty from book");
}
catch(Exception e){
System.out.println("Error opening connection: " + e);
}

try {
String bc = "AAA";
String bn = "BBB";
String ba = "CCC";
int baq = 1;

String query = "Insert into book Values('" + bc + "', '" + bn + "', '" + ba + "', " + baq + ")";
System.out.println(query); // this should looks like your commented line below

int result = st.executeUpdate(query);

if(result==1)
System.out.println("1 record inserted");
else
System.out.println("Sorry...Unkown error");

rs.first();
}
catch(SQLException e){
System.out.println("Error inserting record: " + e);
}
}

}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

Since you are performing the query prior to executing the insert, why would you expect the result set obtained before the insert to reflect a record that you added after obtaining it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Minh Phan
Greenhorn

Joined: Sep 23, 2004
Posts: 8
Thanks! But what is the way to design an interface by which the user can move through all records as well as update and delete some of the?
I appreciate reply from you all!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

Look at the Javadoc for ResultSet and you will find methods (assuming JDBC 2) that answer your questions.
 
GeeCON Prague 2014
 
subject: Insert and MoveFirst