Results 1 to 5 of 5
- 05-02-2011, 11:36 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Having problem on retriving results from a query
Hello suppose that i have the following sql table
name clients
field 1 c_id varchar(8)
field 2 remaining_amount smallint(6)
i need to execute the following statement from a java program and take the results
SELECT remaining_amount FROM clients WHERE c_id='something'
i tried the following code but i keep having an exception
[Java Code:public static boolean checkAmountAndWithdraw (String id,int amount) throws SQLException { String chId = null; Statement stmt ; ResultSet rs ; PreparedStatement pstmt = dbConnection.prepareStatement("SELECT remaining_amount FROM clients WHERE c_id='"+id+"'"); rs=pstmt.executeQuery(); Boolean exists=false; if(rs.getInt(2)==0){ System.out.println ("is") ; exists=true; } System.out.println (exists) ; rs.close () ; return exists; } }
- 05-02-2011, 11:44 AM #2
You have mistake in prepared statement.
see Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)Skype: petrarsentev
http://TrackStudio.com
- 05-04-2011, 10:32 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Do you not think it might be an idea to actually tell us what the exception is (full stack trace and all)?
Your statement isn't actually incorrect...but it is a waste of a PreparedStatement (even an abuse). Reading the link Petr supplied will show you how to use it properly. The way you have it it may as well be a Statement.
However, the error is probably:
You are only selecting one column in your query, whereas that line above is trying to find the third column you've requested...which doesn't exist.Java Code:if(rs.getInt([B]2[/B])==0){
- 05-18-2011, 05:36 AM #4
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
Yeah Petr is right. you have a problem in Prepared Statement
put it in this way
PreparedStatement pstmt = dbConnection.prepareStatement("SELECT remaining_amount FROM clients WHERE c_id= ? id = ?);
pstmt.setString (1,"pass the c_id");
pstmt.setInt (2,pass the id)
while (rs.next()){ write the condition witch you want to check
}
- 05-18-2011, 09:21 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
problem in retriving string data using jsp
By Manas Das in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 02-02-2009, 08:53 AM -
how to take query results to a jlist!!
By themburu in forum JDBCReplies: 1Last Post: 06-07-2008, 10:51 AM -
how to take query results to a jlist!!
By themburu in forum New To JavaReplies: 3Last Post: 06-07-2008, 10:45 AM -
Retriving image from database
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks