File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes access DB using jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "access DB using jsp" Watch "access DB using jsp" New topic
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: 60782
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

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: 30123
    
150

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
 
Similar Threads
result set access with multiple tables, same column name
Hibernate SQL error
getting java.sql.Timestamp exception
newbie: passing an object from a servlet to a jsp
java.lang.OutOfMemoryError