从数据库读取数据时显示为小数值?

huangapple go评论58阅读模式
英文:

When reading data from database it shows decimal value?

问题

我正试图从数据库中读取数据,数据显示为十进制值。

数据库中的projectno字段是varchar类型,值为10637。

但是当我检索它时,输出显示为10637.0 项目号

myConn = dataSource.getConnection();

String sql = "Select * FROM jtc_list  where jtcno='" + jtcnum + "' and materialclass='" + matclass + "'";

// 创建sql语句
myStmt = myConn.createStatement();

// 执行查询
myRs = myStmt.executeQuery(sql);

while (myRs.next()) {
    projectno = myRs.getString("projectno");
    System.out.println(myRs.getString("projectno") + " 项目号");
}

如何解决这个问题?提前谢谢您。

英文:

I am trying to read data from data from database it shows as decimal value.

The projectno field in database is varchar and the value is 10637.

But when i retrieve it shows the output as 10637.0 project no

myConn = dataSource.getConnection();

String sql = "Select * FROM jtc_list  where jtcno='"+jtcnum+"' and materialclass='"+matclass+"'";

// create sql statement
myStmt = myConn.createStatement();

// execute query
myRs = myStmt.executeQuery(sql);

while(myRs.next()){
   projectno = myRs.getString("projectno");
   System.out.println(myRs.getString("projectno")+" project no");
}

How can I solve this issue? Thank you in advance.

答案1

得分: 2

首先,您在MySQL表中的字符列中可能不应该存储数值数据。除此之外,您说道:

> 而且该值是10637

如果文本值确实是10637,那么您应该绝对会看到以下的打印语句:

10637 项目编号

如果您看到的是:

10637.0 项目编号

那么这意味着您将10637.0以文本形式存储在该列中。

在这种情况下,一般最好的做法是:

  • 将数值数据存储在数值列中(例如INTDECIMALNUMERIC)。
  • 从表中检索数值数据(例如整数数据)时,请使用ResultSet#getInt()
  • 如果您想以特定方式查看数值数据,那么要么从MySQL查询中处理格式,要么在Java代码中进行处理。
英文:

First of all, you should probably not be storing numeric data in a character column in your MySQL table. That aside, you said:

> and the value is 10637

If the text value literally be 10637, then you should absolutely see the following print statement:

10637 project no

If instead you saw:

10637.0 project no

then it means you stored 10637.0 in the column as text.

The general best thing to do here is:

  • Store numeric data in a numeric column (e.g. INT, DECIMAL, NUMERIC).
  • When retrieving numeric data, e.g. integer data, from your table, use ResultSet#getInt().
  • If you want to view your numeric data a certain way, then either handle the formatting from your MySQL query, or do so from your Java code.

huangapple
  • 本文由 发表于 2020年9月2日 17:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63702389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定