how can i edit the records on the JSP for varrying colulm
Hello All,
I am displaying the following Database records in a Table of my JSP
page.
Customer No. FirstName LastName(these may be varry depends upon the some other table selected)
1 Tim Hertal
2 John Rizzo
3 Todd Steven
I want to select anyone of the record and perform Edit operation. When
I press Edit button, a new screen open up and take all values of the
selected row. Where I can make changes and send the data back to
database.
I am wondering if someone can help me or sends Java and JSP code or
provides any link where I can find information.
Thank you.
# 1
what puzzles me is that you were able to figure out how to write this jsp to display all records, but now you do not know how to create a page which only displays one of these records.
The key concept is simple enough: you need something to identify that one row you want to edit and you need to send that unique identifier to your edit page. Customer no. seems like a suitable candidate to send as a parameter to your edit page so you can select that customer.
# 2
thanks for reply.
puzzle is that when i show the records i dont know which column is primery key.(customer no)
suppose i choose user table then it display column name as >>>userid,username,.....
in case of customer it shows like customer no.
therefore how can send the whole tuple to next JSP for edit the record
# 3
if you want the code it is as follow
ArrayList columnList =(ArrayList)request.getAttribute("columnList");
ArrayList sysTableDetailsList=(ArrayList)request.getAttribute("sysTableDetailsList");
System.out.println("request value is"+columnList.get(0));
%>
<tr>
<%
for (int i = 0; i <columnList.size(); i++)
{%>
<td nowrap class="tablehead"><%=columnList.get(i) %></td>
<%}%>
<td nowrap class="tablehead"> </td>
<td nowrap class="tablehead"> </td>
</tr>
<%
Iterator itr = sysTableDetailsList.iterator();
while(itr.hasNext())
{%>
<tr>
<% Object[] sysObj = (Object[])itr.next();
for (int i = 0; i <columnList.size(); i++)
out.println("<td>" +sysObj+"</td>");
%>
<td>
<img src="/images/icon_edit.gif" onmouseover="this.style.cursor='hand'" alt="Edit"'></td>
<td><img src="/images/icon_deactivate.gif" onmouseover="this.style.cursor='hand'" alt="Deactive"'></td>
<tr>
# 4
Please use code-tags ( select all the text you want to enclose and click the code button on top of the message window while posting ), it makes things much easier to read.
There must be some way to identify which table it is, right? I mean, you can't just know dynamically which of the colums is going to be the the primary key. In any case, in the edit page too, you'd have to run the query to fetch the data to be edited? Or even if you're fetching the data from the previous page ( not a good idea, IMO ), you'd have to update the correct table?
So, again, you must know which table you're dealing with and hence, the corresponding primary key of that table.
I would put some sort of identifier on the display page to tell which table I was dealing with and then switch-case to find the primary key and then work with that...
I'm still not sure though, what you're getting at... :)
# 5
thanks for reply.
yes,i have to select the particular table from combo box in jsp on the basis of that i have fetch the records from table and column name .
so if edit the record then i would like to make an action set the hidden fields the table as you say but still i don't know the primary key which is not editable at some other jsp page which open after the edit action
# 6
Ok, I did a little bit of Google'*** and here's what I came up with.
You can use the DatabaseMetaData object to find out which columns are the primary keys. See more here: http://www.devx.com/dbzone/Article/27992/0/page/2
Basically:
DatabaseMetaData databaseMetaData = con.getMetaData();
ResultSet primaryKeys = databaseMetaData.getPrimaryKeys(null, null,"training_calendar");
while (primaryKeys.next())
{
String primaryKeyColumn = primaryKeys.getString("COLUMN_NAME");
System.out.println("Primary Key Column: " + primaryKeyColumn);
}
I tried it out on one of the tables that I have, and I got the following results:
Primary Key Column: CAL_Date_End
Primary Key Column: CAL_Date_Start
Primary Key Column: CAL_TRG_Course_Code
Primary Key Column: CAL_Venue
Those are my primary keys, but out of order.
That should help you out right? You know your table name, so you can find out which columns are the part of the primary key.
Edit:
Hey! What's the big deal? I typed in Google' i n g ( w/o the spaces ) and it *'d them out! Abuse protection? But what is "i n g "? ( Yes, I still have to read the Forum Guides :D )
Message was edited by:
nogoodatcoding
# 7
Thanks once again for your reply,
i have used DatabaseMetaData to find the primery key but it works for some table only... due to my database PostGreSQL which is open source or open source Portal which i used.
please give me some idea how to edit the row .......if you have...
i am trying on google but not getting....
thanks in adv...
# 8
I've never used PostGreSQL so I can't really say. But most people seem to doing fine with it and since you say it is working for some tables, I would suggest:
1. Make sure the table's you're not getting for do really have a primary key. Maybe that's why you're not getting any ( I had this problem initially, I didn't have a primary key and I was wondering why it didn't work )
2. Make sure you're giving the correct table name, I dunno if case-sensitivity is an issue, but make sure.
3. Try giving the catalog and schema names also, instead of passing null.
4. Make sure you have the latest version and/or patches.
5. Try using getBestRowIdentifier() ( I dunno about this though )
6. If nothing else works, you'll have to hardcode the primary key column names somewhere and then run a switch-case or something on the table names to idenitfy which colums are to be used as the primary colums.
Most probably though, it's some little thing you've missed out in your code or that the tables don't have a primary key at all, maybe just a column that has a unique index or something.
# 9
i am trying 3 possibility but dint try method getBestRowIdentifier()
i am trying it thanks for your suggestion .....but if i am going to next step for editing then i am facing the problems that how i can edit the values of fields that i dont know how can set the values for edited fields,
is there i used Map to set in action class ...i dont know.actually i am new for struts dev
please help
thanks once again for reply
# 10
PostgreSQL (and Oracle) doesn't support JDBC generatedkeys/primarykeys. Look for another DB's (MySQL, DB2, etc) or another solutions (using hardcoded SQL queries through Statement#executeQuery()).
# 11
@BalusC, but then how is he getting primary keys for some tables? Isn't that kind of weird? Either he should not get keys or he should. Why this kind of behaviour? Under what conditions would the primary key be returned under PostgreSQL?
# 12
if i am using the Hibernate for persitance layer then any method i wil get the primary key and records that i display dynamicaly?
from hibernate i have the session we can esaly get connection or statement ,resultset ...or databasemetadata.....then since I have to used postgreSql ..so there is no chance to change the DB please give me some idea ............
# 13
Why don't you work on this another way? Instead of dynamically trying to find out the primary keys, why don't you keep track of the primary keys of the tables by hardcoding them into your code? Do you know the columns of the tables before hand? As in, not before the page loads but do you have a list of the tables that your application uses? It'll be clumsier but it'll work.
# 14
Hibernate makes use of mapping xml files where you can predefine all kind of relationships, including primary keys.
# 15
> Hibernate makes use of mapping xml files where you
> can predefine all kind of relationships, including
> primary keys.
Exactly,You have to define mappings between your Pojo-DB Table inside
XXXX.hbm.xml files where you ought to know which is you Primary Key or a Unique Identifier inside DB table.
therefore using hibernate would do no good because even hibernate has to use JDBC @last. :)
one cannot generalize your requirement until you have a primary key(index) your Table(Object).
Other than what my fellow poster suggested about usage of DatabaseMetaData.getPrimaryKeys.
Most of the people use autogenerated keys which would be incremented automatically.I'm sure POSTGRESQL supports as i've created such a table before.:)
therefore,it is very easy to know whether the applied column (key) is being autogenerated or not which could be known either by using DatabaseMetaData or a simple ResultSetMetaData.isAutoIncrement(int column) or make use of ResultSetMetaData.isSearchable(int column) methods to do it.
and more importantly which try to download most recent version of JDBC driver which POSTGRESQL provides.
http://jdbc.postgresql.org/download.html
REGARDS,
RaHuL
# 16
thanks for reply ...cool.....I dont know the column of the table even the number of table because the number of table can increase in future so how can i hard code the primery key .Message was edited by: softplusajju
# 17
thanks Rahul For reply.
i checked my xxx-hbm.xml file where the entry of the entity and there primary key and other attribute is present.since there is two thing in portal one is portal and other is ext env where i will develop my project. since i am using both type of table one is own (customize table which i have needed as our application)and other is from portal table.
so there is two entries xxx-hbm.xml . one is ext-hbm.xml and other is portal-hbm.xml
i am agree with suggestion and i think i would work .
If i entered the other table entry(which table that i cant get the primery key) and primary key in ext -hbm.xml file but the problem is when i have entered surely give duplicacy error because when i deploy in server the server finds two entry of the same entity one from portal and other from ext.what can I do....because in initial stage i found this prob beacause i have entered same entity in ext and portal both .
null
# 18
Hi,
Let me give a try logically.
1. Have a Table which will maintain TableId TableName and its primary key or unique key.
2. Use this table to populate your tables combo where you have TableId as your value in Combo and Table name as Combo text.
3. Pass the Table Id to your Editing window and get the Primary Column using this TableId.
Since some database has problem with Finding primary keys. Use this logic in common. With databases which has provision for finding primary key, Use a preload DB script to populate this table. For others have some manull method to populate this table. This should avoid problem with DB migration also.
# 19
after editing the record i have to save the data in db since i have used a new JSP page for editing the record on which i have to perfrom save action but i am facing problem how can make Dynamic varring form or model to get the value from JSP page in action or any other way i have do this...please help ....