File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problems Inserting Info. into Access DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problems Inserting Info. into Access DB" Watch "Problems Inserting Info. into Access DB" New topic

Problems Inserting Info. into Access DB

Kassi Hill

Joined: Sep 29, 2003
Posts: 24
I am trying to insert information into an Access Table. If I hard code the variable information in, the insert statement works but when I assign the value of the ID number into a method to get a new number the update statement does not work. Any ideas??
This is the method I am calling to do the update:
public void addObject(){
int qID = getNewQID();\\get new ID number
System.out.println(qID);\\Used as a check and this is working
String qText = "Does this work yet?";
String qType = "false";
int qCorrect = 2;
String sql2 = "INSERT INTO Question(qID, qText, qType, qCorrect) VALUES("+ ID + ",'" + qText + "'," + qType + "," + qCorrect + ")";
//Send insert query to DataAccess Class to process
This is the method to get the NewID number:
public int getNewQID(){

This is the method that should update the information in the Database:

public void runUpdateQuery(String sql){
openConnection();//Open the Connection
stmt = conn.createStatement();//Create Statement Object
int s = stmt.executeUpdate(sql);
closeConnection();//Close Connection
}catch(SQLException sqle) {
System.out.print("Error in DataAccess: ");
}//end try/catch
}//end runUpdateQuery
[ November 03, 2003: Message edited by: Gregg Bolinger ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300

I beleive I have mentioned to you before about the use of UBB Code Tags when displaying code in your posts.
I just didn't know if there was a reason you have failed to do so. It makes it really hard to read your code when you don't use these tags. I am going to edit your post again so you can see the difference. Please begin to use these code tags.

GenRocket - Experts at Building Test Data
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Please make sure that the code is correct

In the insertstatement you use "ID" and not qID!?. Is there a misspell or what?
// Hope this helps

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
Kassi Hill

Joined: Sep 29, 2003
Posts: 24
Thank you for your suggestions and I sorry I did not use the correct tags when posting my code. Actually, what my problem turned out to be was I was not closing my connection to the database in the right place. Thank you again for your help.
I agree. Here's the link:
subject: Problems Inserting Info. into Access DB