Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Database query result producing null values  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so this is my code it compiles and runs but it does not produce the correct output. any idea why??
 
Ranch Hand
Posts: 506
12
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case the getUserID() method (looks like it is doing that) is to insert into two tables UserStocks and Stocks there should be two executeUpdate statements. Nested try-catch blocks are not necessary.

All the JDBC code including executeUpdate need to be in try-catch blocks. Also, each PreparedStatement should be closed after its execution. The method name getUserID() can be changed to a more appropriate name like insertStocks().

Oracle's Java JDBC tutorials have good examples of how the API is used.

EDIT: It looks like you are not capturing the stock symbol (its value will be null in the database tables) in the posted code.
 
Saloon Keeper
Posts: 4477
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it does not produce the correct output


What output did you expect, and what output did you get?


            catch(Exception e)
            {
            }


An empty catch block is rarely a good idea - at the very least log any exceptions to where you will see them. Otherwise, how will you know what problems there are?
 
Yogi Permaul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output i'm getting is null when running the application.
The output is supposed to be of the stocks from the database
 
Tim Moores
Saloon Keeper
Posts: 4477
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to delve a little deeper into the code - which print statement prints that "null"?

Be aware that line 27 prints out two variables that haven't been initialized, so if those are null, that's to be expected. I also note that the getUserID method does nothing of the sort - it retrieves no data from the DB. That looks wrong.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!