如何将一个类的变量插入到具有JFreeChart的类中以绘制此变量?

时间:2017-02-23 15:09:25

标签: java jfreechart

更新问题[更新] ** - 代码中 Termometr shows 0 - screen

为什么我无法将temp中的变量DBData.java插入Tempo.java中的final DefaultValueDataset dataset = new DefaultValueDataset(new Double(temp)); [我想将其放在数据集中,我想要像termometr一样绘制图表来自DBData.java的值temp

我知道在void ConnectDB() TemperaturaPow中的DBData.java是String,所以我将它解析为double。 我尝试将temp解析为整数但没有结果。

在Tempo.java中,我可以使用System.out.println(dbData.temp),IDEA显示示例9.0,但JFreeChart Termometr显示值" 0"零

Tempo.java

public class Tempo extends ApplicationFrame {

public Tempo(final String title){

    super(title);

    DBData dbData = new DBData();
    final double temp = dbData.temp;

    final DefaultValueDataset dataset = new DefaultValueDataset(new Double(temp)); <<<-------

    final ThermometerPlot plot = new ThermometerPlot(dataset);
    final JFreeChart chart = new JFreeChart("Termometr",JFreeChart.DEFAULT_TITLE_FONT,plot,false);

    plot.setThermometerStroke(new BasicStroke(2.0f));
    plot.setThermometerPaint(Color.lightGray);

    final ChartPanel chartPanel = new ChartPanel(chart);
    setContentPane(chartPanel);
}
public static void main(String[] args){
    DBData dbData = new DBData();
    dbData.ConnectDB();
    final Tempo demo = new Tempo("Thermometr demo");
    demo.pack();
    demo.setVisible(true);
   System.out.println(dbData.temp);

}

}

DBData.java

public class DBData {

    public double temp;
    private Connection con;
    private Statement st;
    private ResultSet rs;

    void ConnectDB(){

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://XXX
                    "passs");
            st = con.createStatement();
            System.out.println("Połączono z BD");

            String query = "SELECT * FROM X ORDER BY id DESC LIMIT 1";
            rs = st.executeQuery(query);
            System.out.println("Records from MySQL");

            while (rs.next()) {
                String TemperaturaPow = rs.getString("TemperaturaPow");
                System.out.println(TemperaturaPow);
                temp = Double.parseDouble(TemperaturaPow); <<<----------
                System.out.println(temp);


            }
        } catch (Exception ex) {
            System.out.println("Error: " + ex);
        }
    }

    void initialize(){
        ConnectDB();
    }

}

[更新] *

我添加了DBData.java中的try以检查它是否解析为double。

while (rs.next()) {
                String TemperaturaPow = rs.getString("TemperaturaPow");
                System.out.println(TemperaturaPow);
                temp = Double.parseDouble(TemperaturaPow); <<<----------
                System.out.println(temp);
                try{
                    temp = Integer.parseInt(TemperaturaPow);
                }catch (NumberFormatException e){
                    System.out.println("Error parse: " + e);
                }
Intellji prints for me :

Error parse: java.lang.NumberFormatException: For input string: "7.7"
0.0
0.0
If I use :

try{
                    temp = Double.parseDouble(TemperaturaPow);
                }catch (NumberFormatException e){
                    System.out.println("Error parse: " + e);
                }

Intellji为我打印:

7.7 7.7 但是thermometr无法绘制 - 它接收null或0?

1 个答案:

答案 0 :(得分:0)

此示例的解决方案是:

DBData.java中,我们有从MySQL = ConnectDB()获取数据的方法。 此方法必须为public double ConnectDB(),此方法必须返回变量temp

public double DBConnect(){
...code...
return temp;
}

在我有代码绘制图表的课程中,我们需要:

DBData dbData = new DBData();
double temp = dbData.ConnectDB();

然后我们可以将变量temp插入到数据集

   final DefaultValueDataset dataset = new DefaultValueDataset(new Double(temp));

工作JFreeChart,如温度计screen of working chart