這裏我計算一列的平均值 如果平均值爲0.0,那麼我想分配字符串變量爲NULL
否則平均價值本身。如何分配空值到一個字符串,然後將其插入到mysql數據庫
這些值存儲在mysql db 現在我的問題是,當平均確實來0.0時,字符串NULL被存儲,但我想要默認的NULL值存儲在它。
如何分配NULL(而不是字符串NULL)到變量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");
}
謝謝! !這工作:) –
不客氣:) – davidxxx