package dbj.jdbc; import java.sql.*; import java.io.*; public class CreateOrder { Connection con; public CreateOrder (Connection c) { con = c; } public void create (int custId) throws SQLException { boolean ready = false; PreparedStatement stmt1, stmt2; int orderNo; try { con.setAutoCommit (false); // Bestellung eintragen stmt1 = con.prepareStatement ( "INSERT INTO book_order " + "VALUES (order_seq.NEXTVAL, ?, " + "sysdate, 0)"); stmt1.setInt (1, custId); stmt1.executeUpdate (); stmt2 = con.prepareStatement ( "INSERT INTO order_item " + "VALUES (order_seq.CURRVAL, ?, ?)"); do { // ISBN erfragen String isbn = Utils.readString ("isbn: "); if (isbn == null || isbn.length () == 0) ready = true; else { // Anzahl erfragen int num = Utils.readInt ("number: "); if (num > 0) { // Bestellposition eintragen stmt2.setString (1, isbn); stmt2.setInt (2, num); stmt2.executeUpdate (); } } } while (!ready); con.commit (); } catch (SQLException exc) { con.rollback (); throw exc; } } public static void main (String[] args) throws IOException { if (args.length != 1) { System.out.println ("usage: dbj.jdbc.CreateOrder customerId"); System.exit (1); } try { Connection con = Utils.getConnection (); CreateOrder app = new CreateOrder (con); app.create (Integer.parseInt (args[0])); } catch (SQLException exc) { exc.printStackTrace (); } } }