Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Problems Inserting Info. into Access DB

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
WITHOUT CODE TAGS
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
dataAccess.runUpdateQuery(sql2);
This is the method to get the NewID number:
public int getNewQID(){
WITH CODE TAGS

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

public void runUpdateQuery(String sql){
try{
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: ");
System.err.println(sqle);
}//end try/catch
}//end runUpdateQuery
[ November 03, 2003: Message edited by: Gregg Bolinger ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kassi,
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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kassi Hill
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Get me the mayor's office! I need to tell him about this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic