Matthew Bendford wrote:
I guess I could go on for quite some time. Also: Don't take it offensive. It's not meant to .
Matthew Bendford wrote:Aside from the couple of "not so optimal code" lines, as for how to iterate through the columns, I use this (although I'm aware that this maybe isn't best practice anymore - as it's something from pre Java9 days):
The important part is the additional = in the comparison. As Rob already mentioned: For some reason someone decided to offset column so it starts by 1 instead of 0 (by at least my guess is this comes from the database folks).
Although this is a "workaround" to just iterate over an maybe unknown column count, when dealing with databases one should do so as closed as possible, as any interaction with external data storage like a database is very error prone and has quite a big possible attack surface.
I guess you currently only do this simple stuff local for your own experience, but the second you have to deal with productive systems there's quite a lot to look out for.
Aside from the couple of "not so optimal code" lines
Rob Spoor wrote:
Kevin O'Sullivan wrote:
Although not related to this issue, you have a SQL injection vulnerability here. While empId and empSalary are safe because they're ints, empName isn't. Someone can wipe your entire database this way.
See https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html for some notes on how to use PreparedStatement with parameters.
There's your problem, and it's probably caused by the annoying 1 offset. You're probably used to loop from 0 (inclusive) to 3 (exclusive). Because you have to start at 1, you have to loop until 3 inclusive or 4 exclusive.
Stephan van Hulst wrote:You never assign the gender field of your Person object.
Lesson: Make your fields final. What point is there in giving the Person class setters?
Another thing, never make your fields public. Provide a getter for the gender field instead.
Campbell Ritchie wrote:Where did you read about hashCode() and equals()? What did they say? I worry about randomly‑found online tutorials; most of them are poor quality.
I recommend you find Effective Java by Joshua Bloch (old edition here), Odersky Sppon and Venners, and search for Angelika Langer Java equals() hashCode(). Make sure to get the English edition of Langer.
Kindly don't delete your questions, neither here nor on the other forum.