Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Insert and MoveFirst

 
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);
}
}

}
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
The knights of nee want a shrubbery. And a tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic