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.
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.
posted 1 month ago
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?
It sounds like you have figured it out, congratulations! Would you mind posting the corrected code so all who read this thread can learn?
All things are lawful, but not all things are profitable.
posted 1 month ago
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.
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