Here I'am calculating the average of one column
If the avg comes to be 0.0, then i would want to assign the string variable as NULL
else the avg value itself.
These values are getting stored in mysql db Now my problem is that when the avg does come 0.0, string NULL gets stored but i want the default NULL value to get stored in it.
How can i assign NULL (and not string NULL) to variable ans??
private void btnAdd1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/check","root","");
st = conn.createStatement();
String sql4 =("SELECT AVG(unit4) as num FROM lo where unit4 IS NOT NULL");
PreparedStatement pstmt3 = conn.prepareStatement(sql4);
ResultSet rs4 = pstmt3.executeQuery();
rs4.next();
double a3 = rs4.getDouble("num");
double b3 = Math.round(a3*10);
double res5 = b3/10;
rs4.next();
avg1.setText(String.valueOf(res5));
String a1 =avg1.getText();
String ans ;
if(a1.equals("0.0")){
ans = null;
}else{
ans = a1;
}
String query = "INSERT INTO chk(id) VALUES ('"+ans+"')";
executeSQlQuery(query, "Inserted");
}