jdbc sql statement

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 8
Reputation: madhusamala is an unknown quantity at this point 
Solved Threads: 1
madhusamala madhusamala is offline Offline
Newbie Poster

jdbc sql statement

 
0
  #1
Mar 11th, 2008
i am getting a problem in updating my database.
actually i am accessing two colums from my table and adding these two numbers ,
and the result i want to insert in the tables 3rd column
The statement i am using to access two columns
String str="select * from emp"
ResultSet rs=con.executeQuery(str);
while(rs.next())
{
int gs=rs.getInt("gross salary");
int al=rs.getInt("allowances"):
int total=gs+al;
con.updateQuery("insert into emp(total) values(total)");
}


i have a doubt about the statement
con.updateQuery("insert into emp(total) values(total)");
is it correct?
please help me out
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: jdbc sql statement

 
0
  #2
Mar 11th, 2008
It should be like this:

"insert into emp(total) values("+total+")" if the total column is type NUMBER in the DB

or like this:
"insert into emp(total) values('"+total+"')"

Use System.out, before you are executing queries in order to see what is executed, so you can use it in an sql editor and debug:

  1. Srtring s="insert into emp(total) values('"+total+"')";
  2. System.out.println(s);
  3. con.updateQuery(s);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jdbc sql statement

 
0
  #3
Mar 11th, 2008
If you are updating an existing record, you should be using
  1. update emp set total=<total> where id=<empId>
You can either concatenate in the values of <total> and <empId> or use a PreparedStatement to set those variables. This means you need to capture whatever you are using for an id on that record in addition to the two values you want to add. Also, there is no updateQuery() in the java.sql.Statement interface. You need to call Statement.executeUpdate(java.lang.String)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: jdbc sql statement

 
0
  #4
Mar 11th, 2008
You shouldn't do it like that anyway.
Use a PreparedStatement instead, with SQL "update emp set total=? where id=?".
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1247 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC