Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert and MoveFirst

 
Minh Phan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Minh Phan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Javadoc for ResultSet and you will find methods (assuming JDBC 2) that answer your questions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic