Get Current logged User Id and User Name using JavaFX

Tags: , , , ,



I’m trying to create an application using JavaFX. I want to get current logged userid and username, after successful login. I want to display it is in the home page. How can i do this? please help

MediaController.java

    @FXML
    private Label tf_getname;

    @FXML
    void happyButton(ActionEvent event) {

        DbConnect dbconnect=new DbConnect();
        Connection conn=dbconnect.getConnection();

        String username = tf_getname.getText();

//        String source1 = event.getSource().toString(); //yields complete string
        //String source2 = event.getPickResult().getIntersectedNode().getId(); //returns JUST the id of the object that was clicked
//        System.out.println("Full String: " + source1);
//        System.out.println("Just the id: " + source2);
//        System.out.println(" " + source2);

        try {

            String sql = "SELECT name FROM users WHERE name='"+username+"'";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                tf_getname.setText(rs.getString("name"));

            }


        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

Answer

I think there is something wrong with your statement. Try out the following way to set up and execute a Statement.

Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("Select * from test");
        while(rs.next()){
            System.out.println(rs.getString("name"));
            con.close();
        }
        }catch(Exception e){
        }


Source: stackoverflow