0

How would i get the "Goal" value from my MySQL into a JLabel? Would i have to separate it into different functions?

Jlabel GOAL = new JLabel(""); 

    try {
        //connection to database
        Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:8889/Daily", "student", "student");

        Statement myStmt = myConn.createStatement();

        ResultSet rs = myStmt.executeQuery("select Goal from User WHERE id=1");

        GOAL.setText(rs.getString("Goal"));

    }
    catch (Exception e) {
        e.printStackTrace();
    }

    GOAL.setForeground(Color.LIGHT_GREEN);
    GOAL.setBounds(23, 90, 61, 45);
    contentPane.add(GOAL);
    `
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MURICA
  • 1
  • 2
  • 1) `GOAL.setBounds(23, 90, 61, 45);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `contentPane.add(GOAL);` No, add the label at start-up. They are invisible so the user won't notice it until text or an icon is set. .. – Andrew Thompson Aug 10 '16 at 07:25
  • .. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code some data to replace the DB. 4) Data from a DB is typically best shown in a `JTable` rather than in a `JLabel`. 5) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 5) *"Would i have to separate it into different functions?"* They're clalled methods, & the answer is no. – Andrew Thompson Aug 10 '16 at 07:27
  • Other than @Andrew Thompson’s comments I should say that `GOAL.setText(rs.getString("Goal"));` looks correct to me. – Ole V.V. Aug 10 '16 at 07:29
  • @OleV.V. Tip: when referencing another person in a comment, start by typing @ & first letter of name. There will be all names starting with that letter in a pop-up on the upper left of the comment entry space. Select the relevant person (by clicking that name) and the name will be automatically inserted (without any spaces). If you try typing the complete name, spelling mistakes are common, like here with my last name. It has a 'p' in it. ;) – Andrew Thompson Aug 10 '16 at 07:40
  • @AndrewThompson Thanks for your help! – MURICA Aug 10 '16 at 07:45
  • 1
    Thanks, @AndrewThompson, for the tip. :-) – Ole V.V. Aug 10 '16 at 11:22
  • Variable names should NOT contain all upper case character unless you are using static constant variables. Your variables except "GOAL" are correct. Be consistent!!! – camickr Aug 10 '16 at 14:46

0 Answers0