| Author |
access DB using jsp
|
Mahesh Lohi
Ranch Hand
Joined: Jun 22, 2009
Posts: 150
|
|
Hi people. I am new to jsps .. I am tring to fatch a value from the DB table and wish to pass it to another jsp using session.
I use
while(rs.next())
{
rs.getString("col1");
}
// Say there are 5 rows in the table. I wish to pass the 3rd row's col1 value that was fatched. I am not able to do this as rs.next value goes to the end of the row i.e 5th at the end of the while loop.
Please help.. I am stuck..
Thanks in advance.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56521
|
|
DB access should not be done in a JSP. If you are just starting out it's best to learn good habits from the start.
This topic has been moved to the JDBC forum since it is not a JSP question.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26489
|
|
Mahi,
You could add a counter and store the value in a variable while in the loop. Then it will be available for later.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Mahesh Lohi
Ranch Hand
Joined: Jun 22, 2009
Posts: 150
|
|
Jeanne Boyarsky wrote:Mahi,
You could add a counter and store the value in a variable while in the loop. Then it will be available for later.
Thanks for that.
I think I was incomplete on that .. Further if all the 5 records are diplayed and the visiter wish to update the 3 row than how will I update that particular row.
Thanks in advance.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26489
|
|
Mahi Lohi wrote: I think I was incomplete on that .. Further if all the 5 records are diplayed and the visiter wish to update the 3 row than how will I update that particular row.
You need to be able to identify which is the "3rd row." To do this, you store the primary key. Then the visitor updates a record and you pass the primary key in to later find the record for updating.
|
 |
bhavesh bhatnagar
Greenhorn
Joined: Jun 15, 2009
Posts: 26
|
|
1)First you have to identify the Row.. based on the (Primary) Key.
2)you have to use Prepared Statements to Update it
PreparedStatement stmt =con.prepareStatement("update tablename set ?(columnName) = ? [,columnxname = newvaluex...]
where ?(columnname) OPERATOR value ");
3) Create an InputStreamReader..
something like this
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
4) use
String col=b.readline();
String ID = b.readline();
int value = b.readline();
//Setting values
stmt.setString(1,col);
stmt.setInt(2,value);
stmt.setString(3,ID);
NOTE:
OPERATORs include:
* = - Equal
* < - Less than
* > - Greater than
* <= - Less than or equal
* >= - Greater than or equal
* <> - Not equal
* LIKE - Allows the wildcard operator, %
hope this will be helpfull
|
 |
Mahesh Lohi
Ranch Hand
Joined: Jun 22, 2009
Posts: 150
|
|
bhavesh evils wrote:1)First you have to identify the Row.. based on the (Primary) Key.
2)you have to use Prepared Statements to Update it
PreparedStatement stmt =con.prepareStatement("update tablename set ?(columnName) = ? [,columnxname = newvaluex...]
where ?(columnname) OPERATOR value ");
3) Create an InputStreamReader..
something like this
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
4) use
String col=b.readline();
String ID = b.readline();
int value = b.readline();
//Setting values
stmt.setString(1,col);
stmt.setInt(2,value);
stmt.setString(3,ID);
NOTE:
OPERATORs include:
* = - Equal
* < - Less than
* > - Greater than
* <= - Less than or equal
* >= - Greater than or equal
* <> - Not equal
* LIKE - Allows the wildcard operator, %
hope this will be helpfull
Thank you it really helped me
|
 |
Mahesh Lohi
Ranch Hand
Joined: Jun 22, 2009
Posts: 150
|
|
Jeanne Boyarsky wrote:
Mahi Lohi wrote: I think I was incomplete on that .. Further if all the 5 records are diplayed and the visiter wish to update the 3 row than how will I update that particular row.
You need to be able to identify which is the "3rd row." To do this, you store the primary key. Then the visitor updates a record and you pass the primary key in to later find the record for updating.
I got the result.. It is working.. Thank you
|
 |
 |
|
|
subject: access DB using jsp
|
|
|