我使用java swing应用程序将一些值存储在mysql数据库中,但未存储全部输入数字。例如,如果我使用float作为mysql数据库列的数据类型,则它仅存储6位数字,如果我输入123456.56,则仅存储123456.00,或者如果我输入12345.67,则仅存储12345.60。同样,当我将mysql的数据类型更改为double时,它仅存储8位数字,因此,如果我输入1234567.89,它将仅存储1234567.90,并且将四舍五入。我的编码有什么问题?谢谢。我想将全部金额存储在数据库中,例如数百万和数十万。 12345678.55或445874.44,而没有将其变成指数值或缩短。我该怎么办?
if(source == btn_confirm)
{
dbconnect connect = new dbconnect();
String page = text_page.getText();
String line = text_line.getText();
String refpage = text_refpage.getText();
String refline = text_refline.getText();
String transaction_date = text_transdate.getText();
String description = text_desc.getText();
String account_code = text_accountcode.getText();
String code_type = text_codetype.getText();
String issue_date = text_issuedate.getText();
String amount = text_amount.getText();
System.out.println(amount);
try
{
connect.addTransaction(page, line, refpage, refline, transaction_date, description, account_code, code_type, issue_date, amount);
connect.close();
}
catch (Exception x)
{
System.out.println(x);
}
text_page.setText("");
text_line.setText("");
text_refpage.setText("");
text_refline.setText("");
text_transdate.setText("");
text_desc.setText("");
text_accountcode.setText("");
text_codetype.setText("");
text_issuedate.setText("");
text_amount.setText("");
text_page.requestFocus();
}
text_amount是包含要传递给字符串量的数字的文本字段。字符串曾经是一个浮点数,但我选择使用字符串代替,因为我认为将其传递到sql语句时不会有太大的区别。我曾经用过parsefloat。
addTransaction方法将事务添加到mysql数据库中,如下所示:
public void addTransaction(String page, String line, String refpage, String refline, String transaction_date, String description, String account_code, String code_type, String issue_date, String amount) throws Exception
{
try
{
connect();
statement = conn.createStatement();
statement.executeUpdate("insert into transaction (page, line, refpage, refline, transaction_date, description, account_code, code_type, issue_date, amount) values ('" + page + "', '" + line + "', '" + refpage + "', '" + refline + "', '" + transaction_date + "', '" + description + "', '" + account_code + "', '" + code_type + "','" + issue_date + "', " + amount + ")");
System.out.println("Inserted transaction of page: " + page + " and line: " + line);
}
catch (Exception e)
{
throw e;
}
}
答案 0 :(得分:0)
您如何初始化该列?如果存储为浮点数,则可以以下格式存储-FLOAT(M,D),其中M是总位数,D是小数点后的位数。
如果您已经创建了表并且不想丢失已经存储在表中的信息,则可以使用以下查询来修改列类型:
ALTER TABLE tableName
ALTER COLUMN columnName FLOAT(M,D);
相应地替换M和D。
答案 1 :(得分:0)
FLOAT
仅具有24位精度。大约等于7个十进制数字。因此,不可能存储比您看到的更多的东西。
FLOAT(12,2)
无用,我无法弄清楚。它四舍五入-一次到十进制,一次到二进制。使用DECIMAL(12,2)
或普通FLOAT
或DOUBLE
。
DOUBLE
可为您提供约16位数字的精度。同样,不要将(m,n)
与它一起使用,也不要期望它可以容纳53个位中的更多数字。
MySQL可以处理65个十进制数字:DECIMAL(65, ...)
。但是,它是固定的,因为您在小数点后指定了固定的位数。