• Post Reply Bookmark Topic Watch Topic
  • New Topic

Make an array from DB results to Send to the JSP Page

 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

After some fantastic help on getting the DB working in java I seem to have hit another stumbling block.

I can now call the DB on the query I want and return results. What I know want to do is build an array of the data, forward it to the JSP page and display the bits individually.

So THis is the Servlet I havce:



This is the JSP Code:



Im using the jstl to call the array that is forwarded from the servlet. Now I originally had it set up as:



However there just seem to show the following error:



I believe it is because of the way I am setting up the Array (which is baffling at best). THis is what I have for the array:



I think It is to do either with the way I am adding the values to User or its the forward:



Any help would be awesome
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to store two different data types and different fields in your ArrayList. It's not capable of that. What you want is a Map (HashMap most likely) which holds key/value pairs. The "ID" field is going to be the key and the "Email" field is the value. You can search these forums or use Google to find lots of examples of loading a Map from a ResultSet and looping through it using forEach.

Incidentally, all that database code really should be in a Data Access Object which would create the Map and send it back to the servlet, but you can work on that step later on. Go ahead and get it working the way you've designed it. Later on we can help you with creating a DAO.
 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Kevin for your response

After some code changes I went for the HashMap as this is as close as I am going to get to associative arrays like in PHP.

I have now changed my servlet to:



The JSP page looks like this:



but things are not quote right.

Basically, I have 2 results from the db query but when i make my HashMap array I am only getting one but both on seperate lines:



It seems like the loop is usiong the 2 fields are 2 seperate loops. This could be because I have my hashmap wrong.

This is the HashMap:



For the life opf me i cannot figure this out. Im googling it to death but not really seeing where i am going wrong
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're getting closer, but still have some work to do. I'll work on getting your code running on my side by cobbling together a similar data source, in the meantime, you need to address a couple of things.

First, I can't figure out why you are adding the HashMap to an ArrayList. That's not doing anything for you that I can tell. You are returning the Map in the request so drop the ArrayList.

Second, you are accessing the Map values wrong. Try something like this.


I'm winging this off the top of my head so if I've given bad information I hope someone else will jump in here. Like I said, I'll work on your actual code so I can better assist you later. Incidentally, in keeping with customary Java naming conventions, "User" and "Users" should be lower case.

Now excuse me, I need to get some coffee and get my brain out of first gear. It's not even 8:00am here yet.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now that I've had some coffee, I've created a very simple example that I think will get you back on the right path. Lets forget all the database access stuff as I'll assume you have that working.

Super simple servlet:

The jsp:

That will result in this html:

Now you should be able to take this information and use it to get your code working.
 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for Helping me out Kevin!

That coffee must be awesome as these examples are way better and make more sense than what I can find.

So I ignored the database call and the loop etc. and just hard coded the values you gave in your example. This is the code I added:



This is the JSP I changed:



The response:



As you can see, worked perfect THank you

Now, I maybe jumping the gun a little so I though I would have a play around and try to get it through dynamically and this is what I did:



Using the bits you had in and bam, it worked! This is my outcome:


Perfect! thats you so much! I did hgave a little error when I first added:



The error was
diamond operator is not supported in source 1.6
. All I did was change the Compiler Compliance Level to 1.7 and update that in the maven pom.

It was such a joy to get it to work, the guys here though I had gone mad

Again, thank you very much to take the time to help me and write some code too. THis has to be my fav forum, such good people here
 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess my next task is to separate out the db connection to only pass the query. Would this be better as a new function below or another class?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Nightingale wrote:I guess my next task is to separate out the db connection to only pass the query. Would this be better as a new function below or another class?

Congratulations on your success and your persistence.

Now you need to move all that database stuff to a DAO, or Data Access Object. It's just a plain Java class that can take any parameters needed such as request parameters passed to the servlet, make your connection, build the Map from the results, and return the results to the servlet. You can find lots of examples by searching here or Googling. Most of if will be cut and paste from what you already have in your servlet. It will have a method called something like getEmailAddresses() that will return the Map.

One tip; remember to think about Separation of Concerns. The servlet shouldn't know or care where the data comes from. All it knows is that it creates an object, calls a method, and gets back a Map of data. Likewise the DAO shouldn't know or care that it's being called from a servlet or Swing gui or that it's part of a web app. All it knows is that it connects to a database, runs a SQL query, builds a Map, and returns it to it's caller.

I'm going to make you work for it because that's the way we are around here. You learn more if you struggle through the steps of doing it yourself instead of someone handing you the solution. Give it your best shot and come back with the results.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Nightingale wrote:All I did was change the Compiler Compliance Level to 1.7 and update that in the maven pom.

Be cautious about this change. If you are in a team environment I'm not sure about the effect this will have when you check in and build the code. I work alone so I can play fast and loose with that sort of thing.

If you want to keep the compliance level at 1.6 just change that line to:

 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank Kevin

Changing to 1.7 is no issue as ATM there is no other code in that place.

Right, I have had a crack at the DAO and kinda hit a stumbling point. I can get the data from the db, build it up etc. but when I return it back to the servlet, its null. So This is the DAO I created:



THis is the code thazt I have in the index class (i need a different name i think):



JSP Page:



As you can see I kept the naming convensions the same but just split the class. I think It could one of three things:

1- im not setting it as an object
2 - not got my return correct.
3 - the call from the servlet to the DAO class is wrong.

Im going to keep playing with it but at least the db stuff still worked
 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, Scrap tha, I only got it working

THis is now my DAO:



THis is now the index file:



This JSP is the same.

Just to check, thats the correct way of doing it right? Or do I need to change anything?

THe one thing that I am concerned about is that the Map is still within the DAO. Is there any way in which I can do this within the index so that It doesnt matter what sql i pass to the DAO, I can do what I like within the index or if I make other files to use it?

Sorry for the millions of questions, Im still new to java
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing well, but there are still a few things that I would change.

Don't pass the SQL from the servlet to the DAO. Notice that you're duplicating code there. The servlet shouldn't care what the SQL code is. If you have parameters to pass, which you don't in this case, that's one thing. The servlet is just the traffic cop, he's directing the flow of things. So your servlet call should just be this:

Never use instance variables in servlets, that makes it non-thread safe, so lose this line:

And put the declaration where you need it in the doGet() method like so:

<edit> I just realized another problem; you are calling that method as static. You need to instantiate an instance of the DAO and then call the method. I'll let you figure that out. It's easy. That one almost slipped by me.</edit>
Now you have a nice clean servlet. Let's take a look at that DAO.

You are always going to return a Map so change the return type. Avoid using the general Object as your return type. And that shouldn't be a static method. Read up static methods and make sure you understand how they are different. Everything else looks pretty good. The only thing I might rethink is the naming. Using userMap instead of userList probably makes more sense and better reflects the api. The method name getSQLResults() is too generic. Something like getUserEmails() or something like that makes for better self documenting code.
THe one thing that I am concerned about is that the Map is still within the DAO. Is there any way in which I can do this within the index so that It doesnt matter what sql i pass to the DAO, I can do what I like within the index or if I make other files to use it?

No, that's where it should be. How else would you return results to the caller? You now have a DAO that can stand on it's own. It can be unit tested and reused by other servlets or even by a Swing application. And all the SQL belongs in the DAO. If you need the DAO to do other things such as insert new records or delete records, you write new methods for that and each method will have it's own SQL statement.

Good work.
 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks Kev for the awesome reply again!

Ok. So I have taken your advbice and moved from trying to make a general call of SQL and make seperate classes for each table I will need to use. THis way I can create different methods inside them and call them from there. However, I have seem to come across an issue.

So I made a new DAO called whiteLabelDAO:



Within this table I want to send back more than 2 values so I added this:



Eclipse keeps complaining aboput it so I added this:



Just throws a wobbly which I expect.

Also the bit you said about this:



Eclipse always throws a wobbly when im trying to get rid of the static bit.

I think I need to create a method and add the sql results into it and then add it to the map?

Im going to google around to see if I can figure it out but I know your the master and any point in the right direct is always welcome
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Nightingale wrote:THanks Kev for the awesome reply again!

Ok. So I have taken your advbice and moved from trying to make a general call of SQL and make seperate classes for each table I will need to use.

It's not so much that you need separate classes as methods. I often have a DAO that will have methods called createNewEmployee(), deleteEmployee(), or modifyEmployee(), but they are in the same DAO class. A parameter passed from the page to the servlet determines which method I want to call.

Stuart Nightingale wrote:
Within this table I want to send back more than 2 values so I added this:


Eclipse keeps complaining aboput it so I added this:



Yes, it will complain about it. You can't do that. A map holds key value pairs. You can't cram 4 values into a Map entry. You have a couple of choices now, both of which are going to require you to do some reading and learning. First, you can do a Map of Lists. The declaration looks like this: Map<String, List<String>>. That will allow you to populate a List of values and then add it to a Map with a unique key. The second option and the one that I recommend, is that it's now time for you to learn about JavaBeans. These are not the same as EJB's so don't freak out. Read up on JavaBeans. They are easy to build and use.
Stuart Nightingale wrote:


Eclipse always throws a wobbly when im trying to get rid of the static bit.

I suspect that's because you are still trying to call it as a static method from your servlet. Are you creating an instance with the "New" operator in your servlet?

 
Stuart Nightingale
Greenhorn
Posts: 20
Linux MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kev, thanks for the response again

Im getting a little stuck between these and just not able to get the array that I would like

So this is the type of array I want:

[0] ->
[ID] -> 1,
[ProductID] -> 20,
[Name] -> Liberty,
[URL] -> http://liberty.com,
[ScreenCSS] -> /branding/GB/stylesheets/screen.min.css ,
[IeCSS] -> /branding/GB/stylesheets/screenie.min.css

I just cant seem to do this. Im also using JSTL tags in order to show them which seems easy enough.

I know im missing something simple......
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused on why you are still trying to use an array. Do those values represent a record from a database?

I'm still on my first cup of coffee, so maybe this will make more sense in about an hour.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!