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

MVC Data access class

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone I am still learning and this is the first time I am working with MVC and data access objects. I have been struggling with this question for a week or so now. I get the exact output that I need to get but the question says the following (Write a Java program to query the StockTracker database for all users and all stocks held by each user. List the user s ID, first name, and last name followed by their stocks, including both the stock symbol and stock description (name). This can be a console application that produces output to the screen, as shown in Figure 11-80. Use the project, StockTracker, to add additional users and stocks, if necessary. For an extra challenge, use only the StockTrackerDB data access class to obtain data from the database.) Ok so my question is this my program draws its information via SQL statements directly from the StockTracker Microsoft database but it doesn't feel firth as that last part of the question states for an extra challenge, use only the StockTrackerDB data access class to obtain data from the database, but every time I try to use the StockTrackerDB  class I don't seem to get it to link to the database. I haven't included any lines of code yet as I don't know the posting rules for code, I don't want to bombard anyone with hundreds of lines of code unless it is allowed to do so here. Thank you I don't expect to get the answers handed to me I just simply want to understand what I need to do to get data from the database via this StockTrackerDB program A point in the right direction so to speak. Please let me know if I must upload my code for you to help me with this and how much I must upload thank you

 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You shoul‍d post as much code as will show the problem. Also please search this forum; there is something familiar about part of your question and you might find that somebody else has had the same problem. By stock, do you mean investments?
I am afraid I am finding it a bit hard to identify the actual problem; is it to do with connecting to the database? If so, please show what code you are using for the database connection and also whether you need a password (whcih you shou‍ld obscure on a public website).
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thank you for your assistance. I have added my working code that draws from the access database itself, I have added the StockTrackerDB as well as the User. I'm not struggling with getting the required data from the access database itself its using the StockTrackerDB as a go between that gives me headaches, basically drawing all user information through the StockTrackerDB which seems to be a static program I keep getting null pointer exceptions ext.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I see that java files are not allowed in attachments so I will send it as text
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this is my program DatabaseQuery



and this is the StockTrackerDB class


 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your last post to put Code tags around the second block of code.

Which line of code is giving you the null pointer exception? Post the full exception message.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StockTrackerDB Class


 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I corrected this method. See comments.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem is when I change my code to connect to the StockTrackerDB class



when I try to link my userID to the database directly I have no problem getting all the user related info in the database like firstName, lastName ext as in my DatabaseQuery code I gave first, that is working perfectly from the database but when I want to connect the StockTrackerDB my results are empty and I think I understand why sort of the StockTrackerDB uses a logon class to input userID and password, but this program has to draw all the information for all three the users in the database but If I say userID = db.getUserID(userID), or userID = user.getUserID(userID). Also the logon and basically all of these other classes are using javax swing whereas this is using a console for output which I think shouldn't be a problem I am sure I am just missing something small.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I use SQL statements to draw the results from the StockTrackerDB class or do I get it from a method in the StockTrackerDB by assigning it to a variable on the programs side and if I use SQL statements isn't it then getting the info from the database directly.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like all SQL should be encapsulated in the StockTrackerDB class. Your application should access the database through this class.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that you are better than the book simplified the Arraylist method which I got straight out of the book nice making it a lot less lines of code
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what I thought to that all SQL should be on the StockTrackerDB side but If I want to change my DatabaseQuery program to draw information via the StockTrackerDB I come up empty handed I am missing something maybe I call the wrong method I am calling the getUser method.
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Please edit your last post to put Code tags around the second block of code. . . . .

I edited his code to add the missing code tags and also shortened the long lines (that is how you do it, but it will be much better if you shorten lines as you go). At least the second class wasn't as bad as the first one for long lines. I hope I don't have to edit it again and make it look different

Don't write System.out.println("). Write System.out.println(). In fact I would prefer to use System.out.printf and use %n a few times. I am not certain, but I think there is a few milliseconds' overhead every time you access the screen, so you will probably get better performance if you have a few big print calls than lots of small ones.
Don't set local variables to null. Let them go out of scope and the garbage collections can happily get rid of the object from memory whenever it is necessary.
Why are you using IO Exceptions when you have a nonexistent user? That looks to me like a scenario where a different sort of Exception might be better.

As CB suggests, the two classes appear to be completely independent of each other. Please tell us the full details about this exception; it is much easier to find if we have the line numbers. Maybe I had better not edit the second post, otherwise all the line numbers will change.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post a snippit of the lines that are coming up empty?

 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . System.out.println( user ); // ASSUMES User HAS A toString() METHOD

Every Object has a toString method. If it isn't an Object then it is null or a primitive, and you can print nulls and primitives, too. You should be able to pass absolutely anything to println and get some sort of output and no Exceptions.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok below is the first problem the first name and last name variables haven't been initialized and also this is drawing from the user class not the StockTrackerDB class



C:\Users\Pieter\Desktop\Java\Java\Chapter11\DatabaseQueryResult\Database.java:47: variable firstName might not have been initialized
System.out.println(userID + " " + firstName + " " + lastName);
                                                         ^
C:\Users\Pieter\Desktop\Java\Java\Chapter11\DatabaseQueryResult\Database.java:47: variable lastName might not have been initialized
System.out.println(userID + " " + firstName + " " + lastName);
                                                                           ^
2 errors

Tool completed with exit code 1
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see that the variables will not be uninitialized, but the compiler can't.  You can clear this up just by giving the variables a default value when declaring them:
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if I try to link the variables of lastName and firstName to db.getUser() I get an error because user returns a User variable not a String variable if I link it to db.listUsers() same story and if I link it to user.getFirstName() it compiles but doesn't print out anything to the console just blank
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not an Exception, but a compiler error. It stems, I think, from your having a main method far too long. I think you should have some sort of class which encapsulates those names (etc) as fields, and a constructor which initialises them.
At present, however, those are local variables. Unlike fields, local variables don't have default values. There is in theory a risk that the local variable is put onto the stack without being given a value and instead you use the previous data on the stack—which is bound to be incorrect. So a rule was introduced to prevent such a disaster: all local variables must be definitely assigned before use. What happens if you never enter the if on line 18? Your name fields won't have values assigned them, and that is not permissible. That would not happen if you have a class. You might end up with the name field pointing to null, but that is a different story. You should be able to organise the flow of control so you get some data out of the database and use those data.

By the way: you will never enter the if on line 18 because of inappropriate use of the == operator.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will cause you grief

When comparing objects like String, you should use the equals() method.
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:. . . System.out.println( user ); // ASSUMES User HAS A toString() METHOD

Every Object has a toString method. If it isn't an Object then it is null or a primitive, and you can print nulls and primitives, too. You should be able to pass absolutely anything to println and get some sort of output and no Exceptions.

I meant a useful toString() method.
 
Rain Draken
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the advice I will work on it tonight and let you all know tomorrow what I have accomplished with this you have all been amazingly helpful instead of giving just code snippets you have given me real advice which is much better thank you
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . I meant a useful toString() method.



But even User@0x1234abcd is better than nothing.
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rain Draken wrote:Thank you for the advice . . .

That's a pleasure Whether that includes thanks for advice on the toString method, well, I shall leave you to work that out for yourself.
 
Crusading Chameleon likes the size of this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic