// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class InsertAfter5 {
    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection con = DriverManager.getConnection("jdbc:sqlite:order.db");
        PreparedStatement opstmt = con.prepareStatement("INSERT INTO myorder VALUES(?,?)");
        PreparedStatement olpstmt = con.prepareStatement("INSERT INTO orderline VALUES(?,?,?,?)");
        Order o;
        o = new Order(13, "M M");
        o.getLines().add(new OrderLine(1300, 13, "A good Java book", 1));
        for(OrderLine ol : o.getLines()) {
            olpstmt.setInt(1, ol.getId());
            olpstmt.setInt(2, ol.getOrderId());
            olpstmt.setString(3, ol.getItem());
            olpstmt.setInt(4, ol.getQty());
            olpstmt.executeUpdate();
        }
        opstmt.setInt(1, o.getId());
        opstmt.setString(2, o.getCustomer());
        opstmt.executeUpdate();
        o = new Order(14, "N N");
        o.getLines().add(new OrderLine(1400, 14, "Another good Java book", 1));
        for(OrderLine ol : o.getLines()) {
            olpstmt.setInt(1, ol.getId());
            olpstmt.setInt(2, ol.getOrderId());
            olpstmt.setString(3, ol.getItem());
            olpstmt.setInt(4, ol.getQty());
            olpstmt.executeUpdate();
        }
        opstmt.setInt(1, o.getId());
        opstmt.setString(2, o.getCustomer());
        opstmt.executeUpdate();
        opstmt.close();
        con.close();
    }
}
