
how to update a column having some value into new column that also have some value i need to add this two value in java using result set we can get all the column data but how to add these value

Hi Friend,
Try the following code:
import java.sql.*;
public class UpdateColumn{
public static void main(String args[])throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from leaveData");
String bal="";
String ide="";
String tbal="";
while(rs.next()){
ide+=rs.getString(1)+" ";
bal+=rs.getString(2)+" ";
tbal+=rs.getString(3)+" ";
}
String idd[]=ide.split(" ");
String balance[]=bal.split(" ");
String tbalance[]=tbal.split(" ");
for (int j=0;j<balance.length;j++)
{
int id=Integer.parseInt(idd[j]);
int b=Integer.parseInt(balance[j]);
int total=Integer.parseInt(tbalance[j]);
int totalBalance=b+total;
st.executeUpdate("update leaveData set leaveTotal="+totalBalance+" where leaveid="+id+"");
}
}
}
For the above code, we have created a table 'leaveData'
CREATE TABLE leavedata (
leaveid bigint(20) NOT NULL auto_increment,
leaveBalance int(11) default NULL,
leaveTotal int(11) default NULL,
PRIMARY KEY (leaveid)
)
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.