In Java, I have defined k as
double k=0.0;
I am taking data from database and adding the same using while loop,
while(rst.
You're still going via double. Stick to BigDecimal everywhere:
BigDecimal k = BigDecimal.ZERO;
while (rst.next()) {
k = k.add(new BigDecimal(rst.getString(5));
}
Alternatively - and preferrably, if the field in the database is actually a decimal value:
BigDecimal k = BigDecimal.ZERO;
while (rst.next()) {
k = k.add(rst.getBigDecimal(5));
}