Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

java - How do I value a label using a database information?

I want to display the information in a database table using multiple labels.

This information for each person is unique and pertains to each row in the table.

After logging in to the program, each person should be able to see their information and correct it if they want, by logging in to their profile page.

For each column of the table, I have provided a label, which displays the information of each person based on the column.

I wrote the following code to display the database data on the label, but when I run it, nothing is displayed!

I also do not know whether to use ResultSet and its methods or Statement and its methods to get the string to use in Label.

enter code here

private void displayProfileDetails(String resultSetQuery, Label label) {

  
    DatabaseConnection connectToDrugDatabase = new DatabaseConnection();


    try (
           
            Connection connectionDB = connectToDrugDatabase.getConnection();
            
            Statement statement = connectionDB.createStatement();
            
            ResultSet resultSet = statement.executeQuery(resultSetQuery);) {

     
        while (resultSet.next()) {

            label Text
            if (resultSet.next()) {

               baseMessageLabel
                Platform.runLater(() -> {

                    label.setText(resultSet.toString());

                } 

                ); 

            } else {

                label.setText("null");

            } 

        } 

    } catch (SQLException | ClassNotFoundException e) {

        e.printStackTrace();

    } 

} 


private void profileDetails() {

   
    String firstNameDisplayQuery = "SELECT * FROM APP.users WHERE USERNAME = '" + userID + "'";

    String lastNameDisplayQuery = "SELECT * FROM APP.users WHERE FIRSTNAME = '" + userID + "'";
   
    String userNameDisplayQuery = "SELECT * FROM APP.users WHERE LASTNAME = '" + userID + "'";
    
    String passwordDisplayQuery = "SELECT * FROM APP.users WHERE PASSWORD = '" + userID + "'";
   
    String EmailDisplayQuery = "SELECT * FROM APP.users WHERE PHONENUMBER = '" + userID + "'";
   
    String AddressDisplayQuery = "SELECT * FROM APP.users WHERE ADDRESS = '" + userID + "'";
   
    String phoneNumberDisplayQuery = "SELECT * FROM APP.users WHERE JOB = '" + userID + "'";
 
    String jobDisplayQuery = "SELECT * FROM APP.users WHERE EMAIL = '" + userID + "'";

   
    displayProfileDetails(firstNameDisplayQuery, firstNameLabel);

    
    displayProfileDetails(lastNameDisplayQuery, lastNameLabel);

    
    displayProfileDetails(userNameDisplayQuery, userNameLabel);

 
    displayProfileDetails(EmailDisplayQuery, emailLabel);

  
    displayProfileDetails(AddressDisplayQuery, addressLabel);


    displayProfileDetails(phoneNumberDisplayQuery, phoneNumberLabel);


    displayProfileDetails(jobDisplayQuery, JobLabel);

   
    displayProfileDetails(passwordDisplayQuery, passwordLabel);

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you call resultSet.next() you moved the cursor ahead. Then you called resultSet.next() again in an if statement and moved it ahead again without getting the value from the first record;

Remove the if statement because if the while loop is running, there is a valid record at the cursor already.

Also resultSet.toString() won't populate the field with your data. You need to use rs.getString(index); where index is the column index (starts at 1) of the column you just got.

Here is an example of what you could do.

// this method returns all columns of a row in ordinal() order. If username is the first column of your table then object[0] will contain a String with that data. etc.      
private Object[] getUserDataByUserName(Connection conn, String username) throws SQLException {
    
            try (PreparedStatement ps = conn.prepareStatement("select * from APP.USER where username = ?")) {
                ps.setString(1, username);
                try (ResultSet results = ps.executeQuery()) {
                    if (results.next()) {
                        Object[] row = new Object[results.getMetaData().getColumnCount()];
                        for (int i = 0; i < row.length; i++) {
                            // arrays start at pos 0, but result sets start at column 1 
                            row[i] = results.getObject(i + 1);
                        }
                        return row;
                    }
    
                }
                return null;
                
            }
    
        }
            

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...