Win a copy of Transfer Learning for Natural Language Processing (MEAP) 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Help with JDBC program

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. So I have another problem. This is mainly just for advice. So I am tasked with making a multiple choice test that retrieves and stores the questions, their particular answers (like 4 at a time) - one of the 4 answers is the correct one. And then I have to generate tests from that information, maybe even randomize it. I have to use postgres and Java.

I am approaching it like this: I have a questions table with primary key of ID (its autoincremented now)  and another field called question that stores the actual question. I also have a answers table with id as primary key and an answer field that stores the answers. There is also a test table with primary key of id, question_id as foreign key to questions id, and user_id as foreign key to user_login(A table that does not affect what I am doing) - There are some other tables but they do not really affect what I am doing.

TO add questions, my idea is to have a GUI with a textfield that will take a question. There is also 4 other textfields that take answers. A checkbox to the right of each will specify the right answer.  So The question is entered with the answers. I put the question in and the id field gets incremented. I now need to get that id field from the questions table and apply it to the 4 answers so all 4 answers have the same id field from questions. This is so I can end up printing out the question and its answers along with the correct answer somewhere. I end up generating the test from the question and it's primary key id value.

What do you think? I am having trouble getting the primary key id from questions and putting it into the answers table.



I need help with lines 19-31

Thank you!
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem with lines 19 to 31 is just that several of them shouldn't be there.

You want to get the generated keys from an INSERT statement. You have an INSERT statement in action at lines 12 to 15. So get the generated keys from that; you already wrote the code to do so and it starts at line 22.

By the way I reviewed some working code I have before putting my foot in my mouth here. And I noticed that PreparedStatement has a getGeneratedKey() method. You might try that instead, if your SQL only inserts one row. Which it looks like it does.
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I made the changes, but still no joy. Can you not change or assign a int value to a primary key or something? it is not on auto increment (im sure this time)  it is not null but that shouldnt matter. No data is being inserted to the answers table.



Not really sure what you mean by the getGeneratedKey thing. I'm using it already, I think. Im using getGeneratedKeys() and there is no getGeneratedKey(). Maybe you could elaborate?

Thank you!
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at lines 28 and 29.

Also, if it were me I wouldn't put up the dialog box until all of the DB processing was finished.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:By the way I reviewed some working code I have before putting my foot in my mouth here. And I noticed that PreparedStatement has a getGeneratedKey() method.



(Removes foot from mouth...)

Turns out that getGeneratedKey() is a utility method that I wrote to encapsulate all of that ResultSet logic which you have in your code. Sorry about that.
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all good. I greatly appreciate your help. I missed something I should have caught. Thank you!
 
Sheriff
Posts: 7052
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you have figured it out, congratulations!  Would you mind posting the corrected code so all who read this thread can learn?
 
Adam Grey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely! I want to give back in any way I can, I appreciate the help.

Completed working code: (I modified it fuirther for my purposes, I am actually taking 4 textbox inputs and putting them *together* into a field in a database. So to get them together I had to change it around a bit more)



It is just a function that groups the answers with the question.

Thanks again!
 
I'm still in control here. LOOK at this tiny ad!
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic