Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with servlet using SQLite

Status
Not open for further replies.

jfdutcher

Programmer
Apr 10, 2001
4
0
0
US
The piece of code below illustrates an issue with the servlet.
The servlet always runs without abending....but it will only either make one 'insertion' OR it will open the database and do the retrieval into a result set....but will not do both in the same execution. Further, when 'inserting' it will only do one insert, the 2nd and 3rd are ignored and it goes to 'close' the connection without doing the retrieval either. If I comment out the 'insertions' and re-compile, it will then do a retrieval into the resultset. So....it will only do one function per run and will not 'fall into' and do anythng else for a given run. Is this a Java language usage issue.....or a problem with the 'sqliteJDBC' JNI classes I am using ?
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName);

// Create a Statement object for the database connection,
Statement stmt = conn.createStatement();
stmt.executeQuery("INSERT INTO name values('John','Dutcher', 0001)");
stmt.executeQuery("INSERT INTO name values('John', Dutcher', 0002)");
stmt.executeQuery("INSERT INTO name values('John', Dutcher', 0003)");
// Create a result set object for the statement
ResultSet rs = stmt.executeQuery("SELECT * FROM name");
// Iterate the result set, printing each column
while (rs.next()) {
String fn = rs.getString("first_name"); // Column 1
String ln = rs.getString("last_name"); // Column 2
String regnbr = rs.getString("regnbr"); // Column 3
out.println("First Name: "+fn+" Last Name: "+ln+" Registration #: "+regnbr);
}
// Close the result set
rs.close();
// Close the connection
conn.close();
 
Inserts are done using executeUpdate() rather than executeQuery.

You never close your statement object either.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top