aspose file tools*
The moose likes JSP and the fly likes Returning a result set in an html form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Returning a result set in an html form" Watch "Returning a result set in an html form" New topic
Author

Returning a result set in an html form

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
I currently have an html form where I can enter data(Add_Product.jsp), which in turn populates a database and then shows another screen(Product_List.jsp) displaying just the Product_ID, title, desc and price of the item.
What I want to do is have a radio button that can be selected next to each Product_ID within that table, which when selected will display the original html form(Add_Product) with the various fields populated. At this stage I want to be able to update any data for that particular product.
How does one go about doing this?


Rob Petterson
SCJP
Pete Harris
Ranch Hand

Joined: Feb 05, 2003
Posts: 39
Without more information, it's pretty hard to explain how to do it, but a few points to move you in the right direction:
1. Why a radio button? Surely a button labelled 'Edit' woulld be better. This button should have an onClick action of AddProduct.jsp?product=<productid>
2. The Add_Product.jsp page should look for the product parameter, and if it exists, get that product from the database.
3. Each form field on the Add_product.jsp page should look whether a product has been found and if so, use that to fill in the value (got from the result set you retrieved in step 2).
4. The JSP where you save products will have to be updated so that it knows whether it is updating of creating a product and alter the SQL accordingly.
Hope this helps.
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks Pete,
I kind of see where you're comming from. You sound like you know your stuff.
I'm completely green to this.
I think you've got an idea as to what I'm trying to build - just a bog-standard ecommerce web site.
The only book I've been using is Beginning JSP by Wrox - not a bad book. I think I need to see more examples of coding for this kind of application. Do you know of any good examples in books / on the web etc?
Pete Harris
Ranch Hand

Joined: Feb 05, 2003
Posts: 39
I do know where you're coming from - I've just finished writing an app that did something very similar.
This problem is conceptually fairly simple - I would start by ignoring the editing idea and just get adding working properly. Once this is there, adding the button to the list page is simple. Then you'll just need a bit of code at the top of add.jsp looking something like

and then for each form field just put someting like this (note the scriptlet inside the value attribute):

This should pretty much do it for you.
As for books, I don't really use them, I tend to learn as I go along so I can't point towards anything in particular. sorry.
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks Pete,
This looks good. I'll try the code when I get home tonight.
I think you're right, the main thing is to get the add_product page 100% working first. It pretty much is except for a browse button I've included in the form. I want to be able to upload an image from my HD into the database, but again I'm having trouble finding the right approach for this as well. All I know is that I have to read a file from my local drive and then add this to the DB, which will in turn store this image as a BLOB.
As for the coding for this, I really don't know.
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Pete,I've tried implementing the code you gave, but I don't know if I'm doing it right:
This is Upload1.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="add.jsp" method="post">
<%
Product_ID = request.getParameter("product");
ResultSet product = null;
if (Product_ID != null)
{
product = /* get the product from the database */
%>
<input type="hidden" name="Product_ID" value="<%=Product_ID%>">
<%
}
%>
<table width="763" border="1" cellpadding="2" cellspacing="1" bordercolor="#99CCFF" bgcolor="#d2e2fb">
<tr align="left">
<td colspan="3"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong><font size="3">Add
Product<font color="#d2e2fb">h</font></font><font color="#d2e2fb">hhhhhhhhhhhhhhhhhhhhyyyyyyyyyyyyyyyyyyyyyyhhhhhhhhhh</font><%=new java.util.Date()%></strong></font></td>
</tr>
<tr>
<td width="156"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product
ID:</strong></font></td>
<td width="590" colspan="2"><input type="text" name="Product_ID" value="<%= ((product!=null)?"rs.getString("Product_ID"):"")%>"> size="11"></td>
</tr>
<tr>
<td><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product Price:</strong></font></td>
<td width="590" colspan="2"><input type="text" name="Product_Price" size="11"></td>
</tr>
</table>
<table width="763" height="140" border="1" cellpadding="2" cellspacing="1"bordercolor="#99ccff" bgcolor="#d2e2fb">
<tr>
<td width="156" height="136"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Current
Product Picture:</strong></font></td>
<td width="253"><img src="coin.jpeg" width=250 height=130 align="middle"></td>
<td width="330"><table width="303" border="0"bordercolor="#124fb1" cellspacing="1" cellpadding="2">
<tr>
<td width="297" align="left"><font color="#d2e2fb">ss</font> <select name="Country">
<option selected>English</option>
<option>Scottish</option>
<option>Irish</option>
<option>French</option>
<option>German</option>
<option>Spanish</option>
<option>Italian</option>
<option>Other European</option>
<option>USA</option>
<option>Canada</option>
<option>Australia</option>
<option>Other World</option>
<option>Ancient</option>
</select> <font color="#d2e2fb">jjjjj</font><strong><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif">Suffix</font></strong>
<input type="text" name="Suffix" size="10"> </td>
</tr>
</table>
<table width="302" border="0" bordercolor="#124fb1" cellspacing="1" cellpadding="2">
<tr>
<td width="148" align="left"> <font color="#d2e2fb">ssssssssssiii</font>
<select name="Grade">
<option selected>Grade</option>
<option>FDC</option>
<option>EF</option>
<option>VF</option>
<option>F</option>
<option>Fair</option>
<option>Poor</option>
</select></td>
<td width="143"><strong><font color="#d2e2fb" size="2" face="Arial, Helvetica, sans-serif">iiii</font><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif">Year</font></strong>
<input type="text" name="Year" size="10"> </td>
</tr>
<tr>
<td height="27" align="left"><strong>
<select name="Type">
<option selected>Type</option>
<option>Hammered Silver</option>
<option>Hammered Gold</option>
<option>Gold</option>
<option>Crown</option>
<option>Double Florin</option>
<option>Half Crown</option>
<option>Florin</option>
<option>Shilling</option>
<option>Sixpence</option>
<option>Groat</option>
<option>Threepence</option>
<option>Penny</option>
<option>Half Penny</option>
<option>Farthing</option>
<option>Maundy</option>
<option>Tokens</option>
<option>Modern Proof Sets</option>
<option>Other</option>
</select>
</strong></td>
<td align="left"> </td>
</tr>
</table></td>
</tr>
</table>
<table width="763" border="1" cellpadding="2" cellspacing="1" bordercolor="#99ccff" bgcolor="#d2e2fb">
<tr>
<td width="155"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>New
Product Picture:</strong></font></td>
<td width="591"><input type="file" value="Browse" size="80"></td>
</tr>
<tr>
<td><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product
Category:</strong></font></td>
<td><input type="text" name="Product_Category" size="65"></td>
</tr>
<tr>
<td height="43"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product
Brief Desc:</strong></font></td>
<td><textarea name="Product_Brief_Desc" rows ="2" cols="68"></textarea></td>
</tr>
<tr>
<td height="139"><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product
Full Desc:</strong></font></td>
<td bgcolor="#d2e2fb"><textarea name="Product_Full_Desc" rows ="8" cols="68"></textarea></td>
</tr>
<tr>
<td><font color="#373c7d" size="2" face="Arial, Helvetica, sans-serif"><strong>Product
Status:</strong></font></td>
<td align="left" valign="baseline"><align ="right"><select name="Product_Status">
<option selected>ACTIVE</option>
<option>INACTIVE</option>
<option>SOLD</option>
<option>SHOWCASE</option>
</select></td>
</tr>
</table>
<table width="763" border="1" cellspacing="1" cellpadding="2"bordercolor="#99ccff" bgcolor="#d2e2fb">
<tr>
<td width="753" colspan="2"align="center"><input type="Submit" value="Submit"/><input type="reset" value="Reset"/></td>
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</form>
</body>
</html>
This is Add.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java"
import="java.sql.*, java.io.*, java.util.*"
errorPage="error.jsp" %>
<jsp:useBean id="coin" class="com.project.databases.Coins"/>
<html>
<head>
<title>Add Coins</title>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("Product_ID"));
float price = Float.parseFloat(request.getParameter("Product_Price"));
String country = request.getParameter("Country");
String type = request.getParameter("Type");
String grade = request.getParameter("Grade");
int year = Integer.parseInt(request.getParameter("Year"));
String suffix = request.getParameter("Suffix");

String productCategory = request.getParameter("Product_Category");
String productBriefDesc = request.getParameter("Product_Brief_Desc");
String productFullDesc = request.getParameter("Product_Full_Desc");
String productStatus = request.getParameter("Product_Status");
coin.connect();
coin.addCoins(id, price, country, type, grade, year, suffix, productCategory, productBriefDesc, productFullDesc, productStatus);
coin.disconnect();
%>

The new coin has been added. <br />
Click <a href="coinlist.jsp">Here</a> To Go Back.
</body>
</html>
Pete Harris
Ranch Hand

Joined: Feb 05, 2003
Posts: 39
Rob,
I'm in a bit of a rush today - got real work to to! But I've had a look at what you've done and I've got a few comments
1. If you haven't got them already, you need the following methods to be available:
  • a bean called Coin or something of the sort which has methods to get all it's attributes getName(), getPicture(), getGrade() etc...
  • a method on the Coins class called getCoin(String id) to get a coin with a particular product id. this should return a Coin object.
  • Coins.add() should update a record if the specified product id already exists in the database.

  • Apologies if I'm re-inventing the wheel and you've already got these methods or similar ones. If so just adapt what I'm saying to what you've got.
    You also need to change a couple of bits of your Upload1.jsp code.
    Firstly, change

    to

    This will retrieve the coin specified by the product parameter if it exists. If not, it will no get do anything and the product bean will be null.
    Secondly, you need to change other scriptlet I gave you. This is 'cos I was stupid and left a typo in it. Therefore change this line

    to this

    oh, and finally you'll need database access from this JSP, so just grab the <jsp:useBean> tag from add.jsp and stick it into this one.
    This should work up to a point. If it does, you'll have to change every form field so that it picks up it's value from the product bean if it exists.
    cheers, Pete
    [ February 12, 2003: Message edited by: Pete Harris ]
    Rob Petterson
    Ranch Hand

    Joined: Jan 23, 2002
    Posts: 149
    Pete, thanks very much for this - especially when you're busy. I'm going to play around with this over the next couple of days. I'll let you know when I've got it working.
    Thanks again
    Rob
    Rob Petterson
    Ranch Hand

    Joined: Jan 23, 2002
    Posts: 149
    Hi Pete,
    Well I havn't got it up and runing quite yet! I need you guidence one again I'm afraid.
    I think I'm close to cracking this, but would sure appreciate your help in tying up some loose ends.
    I've implemented the code you kindly supplied and have done some modifications to some methods in my coin bean class, plus parts of your code.
    To give you an idea of what my site looks like:
    Its' set up using frames. I have 2 buttons on the left hand side of the screen which are Add Coins (if you click this, upload1.jsp appears on the RHS).The other button is Update Coins(brings up Coinlist.jsp).Within Coinlist there is a radio button next to each entry. If you click a radio button and then click on the "Update checked coin" button", it should then make upload1.jsp appear with the relevant data inside each field ready for any editing.
    Q. Can I use this upload1.jsp code or do I have to duplicate it at the bottom on the Coinlist.jsp file?
    If you havn't already guessed it, I'm getting into a bit of a muddle with the logic here.
    Here's all the files that are relevant to this:
    Coins.java

    Upload1.jsp

    coinlist.jsp

    add.jsp

    update.jsp

    [ February 13, 2003: Message edited by: Rob Petterson ]
    Pete Harris
    Ranch Hand

    Joined: Feb 05, 2003
    Posts: 39
    Rob,
    I'm afraid I've been busy recently and will be for most of today, and so I haven't got time to look at it right now. I will definitely have a play with it in the next couple of days, though.
    There again you may have fixed it yourself by then...
    cheers, Pete
    Rob Petterson
    Ranch Hand

    Joined: Jan 23, 2002
    Posts: 149
    Hi Pete,
    I was just going to post you when I saw your post.
    I think I've ironed out most of the problems now and I think I have a better grasp of the logic too.
    I'm going to try and crack the last of it this weekend, baring in mind that the misses dosn't drag me down the shops too long tomorrow clothes shopping.
    I'm just getting one error now, in this bit of code within the coin bean
    :

    Im getting the exception:
    Could not execute the query
    The value I'm passing into the method is the Product_ID generated from clicking a radio button for that relevant record.
    I've tried forcing the method by replacing the'?' for a real id value. When I compile and run the program again, I get different exception saying, 'error occurred before ResultSet'.
    Oh well we'll see, I'll just keep plugging away at it.
    [ February 14, 2003: Message edited by: Rob Petterson ]
    [ February 14, 2003: Message edited by: Rob Petterson ]
    Pete Harris
    Ranch Hand

    Joined: Feb 05, 2003
    Posts: 39
    Rob,
    Excellent - glad it's looking better.
    I think your SQL problem is to do with the fact you're confusing a statement with a prepared statement. You need one of

    or

    My JDBC is a bit rusty so I could be wrong.
    Let me know how it all works out...
    cheers, Pete
    Rob Petterson
    Ranch Hand

    Joined: Jan 23, 2002
    Posts: 149
    Hi Pete,
    Hope you had a good weekend.
    I've tried various combinations of PreparedStatement or Statement, but to no avail.
    In the code listed bellow im getting an exception thrown: java.sql.SQLException: Before start of Result Set.
    I've obviously tried the variations you provided for this method, but again its throwing the first exception in the catch block within the method.
    BTW, I have executed SELECT statements from the dos promp directly from the DB and it does give a result back. This is an example of what I entered, baring in mind that I explicitly gave Product_ID a value that I had already defined previously when entering data into the html form:
    SELECT * FROM Coin WHERE Product_ID =4444;
    I had to change the method in the update.jsp form from getName() to getString(), as it wouldn't compile before. I also did some changes in the code within this file.
    You suggested this:

    But it wouldn't compile, so I changed it to this:

    The above compiles, but to be honest with you I don't know if I've done it right.
    Here's the complete listing of all relevant coding:
    Coins.java

    update.jsp

    upload1.jsp

    coinlist.jsp

    update.jsp

    Pete, it's got to be something pretty straight forward that I just skirting around and not nailing down.
    Pete Harris
    Ranch Hand

    Joined: Feb 05, 2003
    Posts: 39
    Rob,
    I've e-mailed you a few files which I've played with.
    cheers, Pete
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Returning a result set in an html form