【问题标题】:Android app doesn't show SQL result as a numberAndroid 应用程序不会将 SQL 结果显示为数字
【发布时间】:2018-06-15 13:55:08
【问题描述】:

我一直在 Android Studio 上开发一个 Android 应用程序,并且我已经达到了它可以工作的程度,并且我能够从我的外部 SQL 数据库中获取数据。 问题是,当我按下我的应用程序上的获取数据按钮时,我得到一个带小数点的数字,它以 E12 结尾。例如,8.41095645265E12。然而,我输入数据库的数字是 8410376016789。 用于特定列的数据类型是 int(255)、PK、NN。我已经尝试将它切换到 varchar,并在我的工作台上的 PK 和 NN 之间切换,但它仍然存在。我的猜测是这不是 SQL 相关的问题。

这是我的活动代码

package com.example.manager.app;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;

public class SeventhActivity extends AppCompatActivity {

ItemAdapter itemAdapter;
Context thisContext;
ListView myListView;
TextView progressTextView;
Map<String, Double> myprojectMap = new LinkedHashMap<String, Double>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fourth);

    Resources res = getResources();
    myListView = (ListView) findViewById(R.id.myListView);
    progressTextView = (TextView) findViewById(R.id.progressTextView);
    thisContext = this;

    progressTextView.setText("");
    Button btn = (Button) findViewById(R.id.getDataButton);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GetData retrieveData = new GetData();
            retrieveData.execute("");
        }
    });
}

private class GetData extends AsyncTask<String,String,String> {

    String msg = "";
    // JDBC Driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    // Example 10.20.30.40:3306
    static final String DB_URL = "jdbc:mysql://" +
            DbStrings.DATABASE_URL + "/" +
            DbStrings.DATABASE_NAME;

    @Override
    protected void onPreExecute() {
        progressTextView.setText("Connecting to Database...");
    }

    @Override
    protected String doInBackground(String... strings) {

        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);

            stmt = conn.createStatement();
            String sql = "SELECT * FROM mythisone.codes";
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                String name = rs.getString("Toode");
                double price = rs.getDouble("Hind");

                myprojectMap.put(name, price);
            }

            msg = "Process complete.";

            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException connError) {
            msg = "An exception was thrown for JDBC.";
            connError.printStackTrace();
        } catch (ClassNotFoundException e) {
            msg = "A class not found exception was thrown.";
            e.printStackTrace();
        } finally {

            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onPostExecute(String msg) {

        progressTextView.setText(this.msg);

        if (myprojectMap.size() >0) {

            itemAdapter = new ItemAdapter(thisContext, myprojectMap);
            myListView.setAdapter(itemAdapter);
        }

    }
}

} //END of MainActivity

有什么我没有注意到的吗? 谢谢

【问题讨论】:

    标签: java sql


    【解决方案1】:

    双打有问题,而且不精确:

               double price = rs.getDouble("Hind");
    

    带小数:

               BigDecimal price = rs.getBigDecimal("Hind");
    

    没有:

               long price = rs.getLong("Hind");
    

    (虽然限于 2^63,约 8*10^18)

    双精度有两个问题:

    • 表示(次要,也适用于 BigDecimal)。然而,BigDecimal 有 toPlainString(),它取消了科学记数法 3E4 -> 30000。
    • 它使用 2 的幂和来逼近一个数字。对于有错误的小数,对于有间隙的大数字(下一个较大的双精度数大于 1)。

    【讨论】:

    • 感谢您的回复。每当我尝试编辑多头价格 = rs.getLong("Hind"); ,它在 myprojectMap.put(name, price); 下面给了我一个错误。它在价格下划线,并说找到了错误的第二个参数类型。
    • 因为地图也映射到Double,做:Map&lt;String, Long&gt; myprojectMap = new LinkedHashMap&lt;&gt;();
    • 当我改变它时,不幸的是应用程序在点击获取数据按钮时停止。
    【解决方案2】:

    返回的号码在scientific notation。

    要了解如何转换为常规 int,请参阅 this question。

    【讨论】:

      【解决方案3】:

      问题解决了! 编辑这个:

      priceTextView.setText(prices.get(position).toString());
      

      到这里:

      priceTextView.setText(String.format("%.0f", prices.get(position)));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多