jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes How to get the value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to get the value" Watch "How to get the value" New topic
Author

How to get the value

Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
Hi all,

I have a problem getting the value of the stm.

In the below code I have used sum to get the added value of the column qty_pcs.

PreparedStatement stm=con.prepareStatement("select SUM(qty_pcs) from inward where parti=?");
stm.setString(1, itm);
stm.executeQuery();

Now how will I get the sum value assigned to a integer variable

Thank you in advance
Ramasamy Anbalagan
Greenhorn

Joined: Dec 14, 2009
Posts: 20

You can try it by following code snippet

int sumval;
ResultSet rs=null;
PreparedStatement stm=con.prepareStatement("select SUM(qty_pcs) from inward where parti=?");
stm.setString(1, itm);
rs=stm.executeQuery();
if(rs.next()){

sumval=Integer.parseInt(rs.getString(1));

}
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
Thank you for the reply
It works
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
Thank you Ramasamy It works..
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Have you Initialized the connection object. Before the statement

PreparedStatement stm=con.prepareStatement("select SUM(qty_pcs) from inward where parti=?");
check whether connection is present or not. You can check by using below code

if(con==null)
System.out.println("not connected");
else
System.out.println("connected");

-Sunil
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
Thank you for the responce
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get the value