my dog learned polymorphism*
The moose likes Servlets and the fly likes Reading from a database with MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading from a database with MVC" Watch "Reading from a database with MVC" New topic
Author

Reading from a database with MVC

Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
I'm trying to read data from my table. I've gotten inserting down. But if I want to return a whole row or rows. Anyone have any CRUD examples. I'm learning JSP/Servlet/MVC and using MYSQL.

Like I said, I've got the insert part down. I would like to move onto Reading data and puting them into objects then sending them to a JSP page for print out. I think I'm stuck building the ojbects and sending them back to the page. I've got beans for everything.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

One thing you do not want to do is start passing resultsets around.

Rather, copy the information into a bean that abstracts what the row is representing. Or a list of these if appropriate. This is the approach that ORM solutions take.

You can just do it by hand using JDBC, or you could learn one of the ORM systems.

In any case, once the beans or list of beans is passed to the JSP, it's easy to deal with them using the JSTL and EL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
There might be some coding errors, but do you see where i'm trying to go with this? I'm new and I'm just trying to get this down.

Bean: Test


Servlet


Data: TestDB


Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
If I'm wanting to fill a list box full of FNames on the return side....Would I just create an ArrayList<FName> and send that array to the JSP page on the client side?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

A few things right off the bat:

Ricky Jay wrote: public void setTValue(String rfid){this.tValue=tValue;}
public String geTValue(){return tValue;}
Fix the typos. If this is not your real code, then be sure to post the real code.

And TValue is a poor choice for a property name. What's T? Pick something better.

url = "/index.jsp?load=loaded";

What's with the query string on the URL? Get rid of it. Any data should be passed from the controller to the JSP via scoped variables.

HttpSession session = request.getSession();

What is the purpose of this statement? Adding needless code just makes things confusing and creates opportunities for errors.

String url ="";

What is the purpose of this statement? Adding needless code just makes things confusing and creates opportunities for errors.

//THIS IS WHERE I'M GETTING CONFUSED AND WOULD LIKE TO EITHER RETURN A OBJECT and Array of Objects... OR A SINGLE VALUE.

What's stopping you?

Your example doesn't make much sense because all you are doing is fetching a single column from the row. In that case, why not just return the string? Why complicate things?

If you want to return a more complex object containing all the row data, adjust your example accordingly.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
I posted the rough code so I could have some one tell me if I'm heading in the right direction with this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

Ricky Jay wrote:I posted the rough code ...

That is one of the best ways to start getting ignored here on the Ranch. If you waste people's time posting code full of errors that don't represent real errors in your code, people learn to avoid getting their time wasted.

It is a good idea to pare code down to the minimum needed to show the problem or concept. But make sure that the code has no errors and reflects the real code.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
Alright, I'll make note of that. Thanks for the reply. Just starting out here...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

The fact that you are making a good effort to use good practices from the outset is a great start.
 
 
subject: Reading from a database with MVC
 
Similar Threads
Design and include
why struts framework?, lifecycle of the framework
How to create a drop down on pageload with data from a db
HELP! Problems with Buffer/Response Committed
Sending data from Server to JSP page