Search code examples
javaswingresultset

get all values from one class to anothed class method


I have an class IntegrationWithDB in which i have to method getConnection()and selectFromDB().

  • In the selectFromDb() i have a result set , i want to get the result set vales in another class method
  • Actually it did but it only shows the last value of dataBase table.
  • Note i have made getter and setter method in IntegrationWithDB class and use in selectFromDB() method.

    public void selectFromDB() {

        try {
            if (this.conn == null) {
                this.getConnection();
            }
    
            if (this.stmt == null) {
                this.stmt = this.conn.createStatement();
            }
    
            int success = 0;
            this.query = "select * from contacts order by node_id";
    
           this.rs = this.stmt.executeQuery(query);
           // something is wrong in the while loop
            while (rs.next()) {
    
                setId(rs.getInt("node_id"));                               // i made getter and setter for id, name, parent and for level
                setNam(rs.getString("node_name"));
                setParnt(rs.getString("node_parent"));
                setLvl(rs.getInt("node_parent"));
    
    
            }
    
    
    
            if (success == 0) {
                this.conn.rollback();
            } else {
                this.conn.commit();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    

and in another class test i have method displayList() in this method i write the following code

public class test {


     IntegrationWithDbClass qaz = new IntegrationWithDbClass();

    public  void displayList ( ) {


       qaz.getConnection();
       qaz.selectFromDB();


       for(int i = 0; i< 5; i++){
        System.out.println(" "+qaz.getId()); 
         System.out.println(" "+qaz.getNam());
       }

          }

when i initilize the displayList() method in the main method , it shows the following result

5
red

how can i get all the five values?


Solution

  • First of all you have to create what is commonly referred to as an Entity class. This is the class that represents a single row in your database. This should ideally be separate from the code that interacts with the database connection.

    So first step, create a class named Contact, and in it put the 4 fields you have, id, name, parent and level, with the respective getter methods. If you do not expect these to change by your program make them immutable, it is the good practice to ensure consistency. So something like:

    public class Contact {
    
       private final int id;
       private final String name;
       private final String parent;
       private final String level;
    
       public Contact(String id, String name, String parent, String level) {
         this.id = id;
         this.name = name;
         this.parent = parent;
         this.level = level;
       }
    
       public int getId() {
         return id;
       }
    
       //... put the rest of the getter methods
    }
    

    Then in your IntegrationWithDB class (I would rename this to something more meaningful like ContactRepository) you can change that method you have to:

    public List<Contact> getContacts() {
       // ... your database connection and query code here
    
       this.rs = this.stmt.executeQuery(query);
    
       List<Contact> contacts = new LinkedList<Contact>();
    
       while (rs.next()) {
    
          int id = rs.getInt("node_id");                               
          String name = rs.getString("node_name");
          String parent = rs.getString("node_parent");
          String level = setLvl(rs.getInt("level"));
    
          contacts.add(new Contact(id, name, parent, level));
       }
    
       //... the rest of your database handling code, don't forget to close the connection
    
       return contacts;
     }
    

    Then from displayList() you just have to call getContacts() which gives you a list of Contact objects to iterate through.