问题
In my project I used txtAdvance 's key event.
double gtotal = Double.parseDouble(txtGtotal.getText());
double ad = Double.parseDouble(txtAdvance.getText());
double due = gtotal - ad;
txtDue.setText(String.valueOf(due));
And after last line run, I add a save button to save those data. Given below is that query.
public void saveInvoice(JTextField txtgtotal, JTextField txtAdvance, JTextField txtDue, JTextField txtInID) {
try {
db.putData("INSERT INTO indetails(inid, gtotal, advance, due) VALUES( '" + txtInID.getText() + "' ,'" + txtgtotal.getText() + "' , '" + txtAdvance.getText() + " ' " + " , '" + txtDue.getText() + "') ");
JOptionPane.showMessageDialog(null, txtAdvance.getText());
JOptionPane.showMessageDialog(null, "Invoice details saved");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, this.getClass().getName() + " first " + e);
}
}
I'm having MySql Data truncated for column 'advance' at row 1 . advance's data type is Double. But I can't find a way to put length.
(As an example
Column Name : iid, Data type : Int(18) {Primary key}
Column Name : inid, Data type : Int(18)
Column Name : gtotal, Data type : Double
Column Name : advance, Data type : Double
Column Name : due, Data type : Double
)
I'm using MySQL query browser . Question is when I'm adding 0 value to txtAdvance I'm having this error.
回答1:
If the data you are trying to save have decimal points then change the datatype for advance as double with length 10,3 where 10 denotes number of digits and 3 denotes the number of decimal digits.
Use this query,
db.putData("INSERT INTO indetails(inid, gtotal, advance, due) VALUES(" + txtInID.getText() + "," + txtgtotal.getText() + "," + txtAdvance.getText() + "," + txtDue.getText()+")");
回答2:
Instead of creating your query by hand, you should use db driver for it:
PreparedStatement putData = con.prepareStatement("INSERT INTO indetails(inid, gtotal, advance, due) VALUES(?,?,?,?)");
double gtotal = Double.parseDouble(txtGtotal.getText());
double ad = Double.parseDouble(txtAdvance.getText());
double due = gtotal - ad;
putData.setInt(1, inid);
putData.setDouble(2, gtotal);
putData.setDouble(3, ad);
putData.setDouble(4, due);
try {
putData.execute();
JOptionPane.showMessageDialog(null, txtAdvance.getText());
JOptionPane.showMessageDialog(null, "Invoice details saved");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, this.getClass().getName() + " first " + e);
}
来源:https://stackoverflow.com/questions/19044527/mysql-data-truncated-for-column-advance-at-row-1