为什么我不能在jframe上显示数据?

时间:2017-01-24 06:32:07

标签: java jlabel

我正在尝试在标签上打印出来,但即使成功连接数据库后也无法打印任何内容。弹出窗口显示但没有显示数据。当我删除rs.next()并用消息显示的任何字符串替换它时,但当我使用rs.next()时,消息变为空。

    String query = "SELECT * FROM OZEKIMESSAGEIN WHERE ID=1";

    Statement st = connection.prepareStatement(query);

    ResultSet rs = st.executeQuery(query);

    JFrame frame = new JFrame();
    frame.setSize(300,125);
    frame.setUndecorated(true);
    frame.setLayout(new GridBagLayout());

    JLabel headingLabel = new JLabel();

    while(rs.next()){
        String value = rs.getString("msg");
        headingLabel.setText("The mpesa message is" + value);
    }

    Icon headingIcon = null;
    headingLabel .setIcon(headingIcon); // --- use image icon you want to be as heading image.
    headingLabel.setOpaque(false);

    JLabel messageLabel = new JLabel();

    while(rs.next()){
        messageLabel.setText(rs.getString("MSG"));
    }

    frame.add(messageLabel, constraints);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);

    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen

    java.awt.Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());// height of the task bar
    frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight());


    new Thread(){
        @Override
        public void run() {
            try {
                  Thread.sleep(10000); // time after which pop up will disappear.
                  frame.dispose();
            } catch (InterruptedException e) {
                }
       };
    }.start();

0 个答案:

没有答案