英文:
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
以文本形式存储在该列中。
在这种情况下,一般最好的做法是:
- 将数值数据存储在数值列中(例如
INT
、DECIMAL
、NUMERIC
)。 - 从表中检索数值数据(例如整数数据)时,请使用
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论