Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access DB using jsp

 
Mahesh Lohi
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
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
Marshal
Posts: 34084
337
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.
 
bhavesh bhatnagar
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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic