0

I am trying to print out on the label but i cannot print anything even after connecting to the database successfully. The pop up shows but no data is shown. When i remove the rs.next() and replace it with any string the message shows but when i use the rs.next(), the message becomes empty.

    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();
jptl431
  • 304
  • 1
  • 6
  • 17
  • 2
    Welcome to SO, Please look at posting a [MCVE] for quicker help! – Ashwin Gupta Jan 24 '17 at 06:34
  • Additional to @Ashwin: When you have a Problem with rs.next() you maybe should include the line of code, where you are initializing rs. Nobody on SO knows, what rs may be. How should we tell you, why it is returning empty Strings? – GAlexMES Jan 24 '17 at 06:40
  • the rs is the resultSet which i am using to extract the data from the database. – jptl431 Jan 24 '17 at 06:45
  • http://stackoverflow.com/questions/940913/prevent-swing-gui-locking-up-during-a-background-task – Viet Jan 24 '17 at 06:49
  • You should add this information to your question. Do you checked, if the ResultSet is empty? Show us the line of code, where you create the query. – GAlexMES Jan 24 '17 at 06:49
  • Hello Jerry, the override is working perfectly. All i have a problem is with the data printing on the screen. – jptl431 Jan 24 '17 at 06:52
  • GAlexMES, I have added the lines where I am initialising the resultset and the query – jptl431 Jan 24 '17 at 06:55
  • You must get data from result set first, after that you init UI, you need read more about EDT in java https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – Viet Jan 24 '17 at 06:57
  • How do i get the data from the resultset? @Jerry06 – jptl431 Jan 24 '17 at 07:02
  • I don't think it is an EDT problem. He is filling the Panel before it is attached to the JFrame. So it is not in the UI-Thread. – GAlexMES Jan 24 '17 at 08:17

0 Answers0