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

Recommended Answers

All 3 Replies

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+"')";
System.out.println(s);
con.updateQuery(s);

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=?".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.