Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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??
 
Rancher
Posts: 506
15
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: 4702
117
  • 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: 4702
117
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!