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);
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

8 Years
Discussion Span
Last Post by jwenting

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:

Srtring s="insert into emp(total) values('"+total+"')";

If you are updating an existing record, you should be using

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)


You shouldn't do it like that anyway.
Use a PreparedStatement instead, with SQL "update emp set total=? where id=?".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.