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.
i think this question is already large enough... actually, I don't even see a question here. Do you have a question?