• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

access DB using jsp  RSS feed

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 67162
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 38906
684
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi,
You could add a counter and store the value in a variable while in the loop. Then it will be available for later.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
author & internet detective
Posts: 38906
684
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!