0

Suppose I have some data in a data base and I am retrieving that using query.

Example:

SELECT  * FROM acsuserdetail where "+useranme+"= '"+arg+"' "
System.out.print(" FirstName = " + rs.getString("FirstName"));

This will return two result i.e.:

FirstName = Anurag
FirstName = Arvind

But when I am showing this data in UI in a JFrame then it is opening two frames having two details and if more details are there then that number of frame will open. This may be because the data which is coming from database are coming one by one not in single shot. I want all information to consolidate in a single frame. Code for UI is:-

public UIForShowingData(String data) {
    frame = new JFrame("Showing Data");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(300, 300);
    button = new JButton("OK");
    frame.setLayout(null);
    button.setBounds(250, 250, 40, 50);
    frame.add(button);
    System.out.println(data.length());
    tx = new JTextField(data);
    frame.add(tx);
    tx.setBounds(50, 50, 40, 50);
    button.addActionListener(new ActionListener() {    
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();        
        }
    });
}
dic19
  • 17,821
  • 6
  • 40
  • 69
OPTIMUS
  • 672
  • 4
  • 14
  • 29

1 Answers1

0

Please note it is unclear what is happening given the code snippet in your question. However I'll take a shot in the dark and guess that UIForShowingData(String data) method is being called for each record in the result set obtained by querying the database.

Something like this:

while (rs.next()) {
    ...
    UIForShowingData ui = new UIForShowingData(rs.getString("FirstName"));
    ...
}

This would explain why there are many frames opened as records are in the data base. To solve the problem you might consider use a Collection to store several values and use this collection to properly show data in a single JFrame. Probably using a JList or a JTable is a better choice than display records using text fields.


Off-topic

Please note your query is vulnerable to SQL injection attaks. To avoid this you may want to try PreparedStatement instead. For example:

String query = "SELECT  * FROM acsuserdetail where useranme = ?";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(arg); // where arg is the argument to compare with
ResultSet rs = pst.executeQuery();

Swing is designed to work with Layout Managers and thus the use of null layout and methods such as setBounds(...), setLocation(...) and setXxxSize(...) are discouraged. From Swing tutorials:

Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.

See also these topics:

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69