• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database query result producing null values

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Siyavuya Marete
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic