Kevin O'Sullivan

Ranch Hand
+ Follow
since May 16, 2018
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin O'Sullivan

Matthew Bendford wrote:

I guess I could go on for quite some time. Also: Don't take it offensive. It's not meant to .

No offence taken at all, thanks so much taking the time to type all that out,it was helpful, I will take your advice on board.

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.

Thanks for replying, Ya its just for practice, I have a local database on my computer.

When you say

Aside from the couple of "not so optimal code" lines

Would you mind expanding on that please? and thank you.

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 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.

Problem was I forgot =

*face palm*

thanks for replying .
I'm very new to java and databases, I'm just messing around looking at how to enter data, I created a table where by you can enter an employee name, id number and salary, the problem is when I run the program at first I was able to see all the data I entered but now I can only see the name row and id number and can't see the salary row,  why is this happening ?

here and example of the output im getting


and this is what I want and was getting


any help appreciated

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.

Thank you !

when you say make the fields final, do you mean like this

3 months ago
Just a small program whereby I add person information to a list and then its printed out to the screen when the loop is exited but gender is always null, why is that ?

Any help appreciated.

3 months ago
Would a better option be just making the scanner instance static in your main class? and then you could use it across multiple classes.
3 months ago
ok thanks for the replies, was just a thought I had.
3 months ago
Just a thought I had, Is it good practice to do this

putting the scanner object in an interface and then allowing any class that requires to take input to implement it?
3 months ago

Campbell Ritchie wrote:Not bad. Can you do it with the removeIf() method?

I'm using java 14 with Intellij and the removeIf() doesn't appear in the drop menu but its been included since java 8?
3 months ago
I got it working

3 months ago
Haven't had a chance to try the above yet but Thanks everyone for all yer help, I ordered the effective java book, can't wait to give it a read, seems to be a popular book.
3 months ago
Thanks a lot for your help, I ordered the effective java book, can't wait to give it a read, seems to be a popular book.
3 months ago

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.


Just they are used to compare objects and return true or false but I don't have any clue how to implement it for what I need to do.


and this video

3 months ago