• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Database not printing third row

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


conor:34567:
john:43256:
kevin:34565:




and this is what I want and was getting

kevin:34565:40000
conor:34567:40000
john:43256:50000



any help appreciated


 
Sheriff
Posts: 22209
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Foreman
Posts: 121
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kevin O'Sullivan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





Problem was I forgot =

*face palm*

thanks for replying .
 
Kevin O'Sullivan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Matthew Bendford
Ranch Foreman
Posts: 121
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of: The following is my very own personal opinion. It may be not good, maybe even wrong or contain some errors in some parts. So, don't take any of the following for pure gold - just read it as "some random advice".

Back to topic: As many here often say: Forget about code for a moment - take a paper and a pen - and write down what it is you'd like to accomplish in your native language. You can do this digitally like a digital scratchbook or even use some text editor to note down your thoughts.
This way you get an abstract view of what may be important, and how to interconnect maybe different possible independed parts.

From what you given us so far I would start with something along these lines:

You'd like to write a small application able to perform two tasks:
1) list current records in a database
2) add new records

With this you already end up with three more or less independed parts:

1) the overall application (so called glue logic)
2) the stuff to get records off of the database and display them
3) the stuff to put new records into the database

From here we can already see what you got at least somewhat in the right way - and what you got quite wrong:

a) You have a main application which is the glue to "drive" the rest of the parts. That's a good start, although the execution is very bad due to B.
b) Although you have two different tasks, one to display current records and one to add new ones, you squeezed both togehter into one class.

In object oriented languages like java one should try to follow this pattern: One class per task. So, I would already end up with 3-4 classes:
1) main
2) menu
3) display records
4) insert new records

First major difference to what you currently got: Your "DataBase" class (btw: bad name here) does three of the four tasks all in one. That's already bad design. Split them up! Write one class for the menu, one class for fetching and display current records from the database (which could be split up even further into one part fetching the data and another one for displaying the data) and a third class for inserting new data (which again could be split up into one class for input and another one for the actual database stuff). Your main class is a good start, although it's procedural style, not object oriented. It's about writing a batch/shell script, not using a powerful OOP language to its potential.

Next: The environment. Do you really use MySQL v8.x, some older v5.x, or maybe even MariaDB? Reason I'm askin: As MySQL switched from 5.x to 8.x it not just changed its license, but also some of the inner workings. Which pretty much forced me to migrate to MariaDB. MariaDB provide its own JDBC connector driver. By the classname and the url in your code it looks like you're using MySQL 8.x.
In fact, it doesn't really matter overall - but it can lead to subtle differences you may run into no one here can help with without additional information like: What database you're using, what version? What connector and version you use? These information may can become important later.

JDBC: Current JDBC drivers are all JDBC 4.x - which means: You don't need this infamous Class.forName() line anymore. Just make sure the JDBC driver is in the classpath. You can just throw out this line without replacement, and also the CNFE catch block.

java.sql: As Rob already mentioned: Although you're currently fooling around - don't even look at regular Statements - use PreparedStatements from the very first line of SQL code. If you ever deal with real world productive systems it's one of the key important security features.
In fact, if I'm your QA and would see you try to check in some new code using lines like this - I would give you just ONE warning about doing it right - the next time I would already ask boss to terminate you - as such code is not just vulnerable - it screams for "attack me".

boolean comparison

NO! If you're lucky and got away with my previous warning - this line would get you fired right away, as it shows lack of very basic knowledge of the used language:

DON'T: if(flag == true)
but: if(flag)
DON'T: if(flag == false)
but: if(!flag)

Comapring booleans with == is error prone - as you could end up missing one of the "=" - which ends up in: if(flag = false) - which is a simple assignment - which always evaluates to true. So, you want to test for if your keepGoing is still true. How about you miss one of the "="? This loop will never end - no matter the actual state of the flag - as you always reset it to true.

connection pooling: You don'T want to establish a new connection to the database, execute your statement, and close it down. You want to establish one connection at application startup - re-use it as long as the application runs - and close it down on temrination. You may not feel the impact on your local test. But imagine how sites like amazon with thousands of requests EVERY SECOND would just come to a complete halt. Establishing a new connection and tearing it down takes a lot of resources and additional traffic each time. Just open one connection and keep using it until you finished is way more performant.


I guess I could go on for quite some time. Also: Don't take it offensive. It's not meant to - it's just objectively pointing out what could be improved to your code. You did nothin "wrong" - but in a very inefficient way. It's understandable as it shows you seem very new not just to Java but to programming overall. Don't get discouraged by my "rant". Take a deep breath, maybe drink some tea, and get the points one after another, one by one. And also take your time to fully understand one point before move on to the next one. Yes, this takes time and effort. But it'll get you quite far even with this very simple example. At least it'll help you on your way onwards instead of doing the same mistakes over and over again so they become a habit. It's rather hard to re-learn the "good" way after you already got your "bad" habits. You better of by going a more optimal route from the begin with.
 
Kevin O'Sullivan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic