Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reading from a database with MVC

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ricky Jay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I'll make note of that. Thanks for the reply. Just starting out here...
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that you are making a good effort to use good practices from the outset is a great start.
 
reply
    Bookmark Topic Watch Topic
  • New Topic