views:

85

answers:

5

In my Web Application, i have a data table(Displayed from DB using PreparedStatement). Each row have an edit button. A form is situated below the table. If i click edit button, the data from the row will be filled into the form. So, that i can edit and save it.

Or, i can enter new data in the form and a add new row. The issue is the "Save" button. Update & Insert needs different queries.

SO, i want to call two different methods. Any idea regarding this issue.

Addtional informations: The web page was built using HTML & Java Script. Java used in back end to connect DB and get Data.

Thanks.

+1  A: 

All you need is a hidden input field that is set to different values initially and after clicking the edit button. Then you can check that field's value and do either an INSERT or an UPDATE in the save action.

Michael Borgwardt
+4  A: 

You could add, in an hiddden field, the id of the edited element. If no id is provided, it's a new object, and if an id is provided, it's an edit operation. Obviously, to prevent spying you should replace real id by hashed ones.

Riduidel
+1  A: 

Are you using any JavaScript library? If you are using JQuery, you can easily bind and unbind events to your button. It also has the concept of data which you can use to attach any meta-data to your controls like the button. Use the meta-data for the mode of the button (edit/save) instead of a hidden field.

Sample code: (during page load it could be an edit)

$("mybutton").data("mode", "edit"); //data is a dictionary for key/values
$("mybutton").click(EditFunction);
Giljed Jowes
+1  A: 

You should consider using javax.persistence.* and appropriate frameworks for database operations.

If you want to stick to pure SQL you should have some kind of status information of your table row. Does it already have a primary key assigned? Is this key generated by the database? Than the row already exists in the database and your method can generate an UPDATE statement. If the key has not been assigned yet use an INSERT.

Another good idea is to encapsulate the result of your query with simple Java Beans used to display the data. The simple objects allow you to store and handle additional data and informations - which can be quiet handy when generating the proper SQL statements.

Daniel Bleisteiner
A: 

Or, i can enter new data in the form and a add new row. The issue is the "Save" button. Update & Insert needs different queries.

Isnt it possible to use same query for insert & update ???

Shekhar
Is that possible? If so means, please explain it here. Thanks
NooBDevelopeR
PLSQL knows a MERGE query that does the trick. Maybe other database too?
Daniel Bleisteiner