1

I'm trying to learn my ways around saving and loading data to/from Databases and I came across a problem. At the moment I have a class called Component that has the following:

public class Componente {

    private int code;
    private String description;
    private double price;
    private int quantity;
    private List<Componente> previous;
    private List<Componente> incompatible;

I also have a class called Storage which has the following:

public class Storage {

    private List<Component> stock;

There are other classes where I was able to create the save and load methods because they only had simple variables like Strings or Integers, but for these ones I'm completely lost because not only are there Lists to be saved/returned but there's also recursive Lists. What I have so far for the Components is:

public void save(Component c)throws SQLException{
    Connection con = null;
    
    con = Connect.connect();
    PreparedStatement st = con.prepareStatement("INSERT INTO component 
    VALUES(?,?,?,?,?,?)");
    
    st.setInt(1, c.getCode());
    st.setString(2, c.getDescription());
    st.setDouble(2, c.getPrice());
    st.setInt(2, c.getQuantity());
    //Missing the last 2 variables
    st.executeUpdate();
    
    con.close();
}

public Component load(Object key) throws SQLException {
    Component c = null;
    Connection con = Connect.connect();
        
    PreparedStatement ps = con.prepareStatement("select * from component 
    where code = ?");
    ps.setInt(1, code);
    ResultSet rs = ps.executeQuery();

    if(rs.next()){
        c = new 
        
       Component(rs.getInt("code"),rs.getString("description"),
       rs.getDouble("price"),rs.getInt("quantity"));
    }
    con.close();
        
    return c;
}

There are exceptions to be handled, but in that part I think I'm good. Also if I'm able to do it for Components I'll probably be able to set it up for Storage too, so for now I think this question is already large enough.

2
  • 2
    i think this question is already large enough ... actually, I don't even see a question here. Do you have a question? Commented Dec 26, 2018 at 14:58
  • 1
    JDBC is very close to raw database access, so main question when unloading related entities with JDBC is "how are they organized in database". You given us no explanation of your database structure, so we can't help you with unloading part. Commented Dec 26, 2018 at 15:11

1 Answer 1

1

Based on what I understood from your question. You might need to make the return type of the load function something like a list; build your list in the rs.next() block and return when you are done.

 public List<Component> load(Object key) throws SQLException { 
     List<Component> componentList= new ArrayList<Component>();
    Component c = null;
    Connection con = Connect.connect();

    PreparedStatement ps = con.prepareStatement("select * from component 
    where code = ?");
    ps.setInt(1, code);
    ResultSet rs = ps.executeQuery();

    if(rs.next()){
        c = new Component(rs.getInt("code"),rs.getString("description"),
                          rs.getDouble("price"),rs.getInt("quantity"));
        componentList.add(c);
    }
    con.close();

    return componentList;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.