This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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?
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.
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?
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.
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, 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
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
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 ]
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
[ February 13, 2003: Message edited by: Rob Petterson ]
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
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 ]
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
Pete, it's got to be something pretty straight forward that I just skirting around and not nailing down.