Advertisement
Guest User

Untitled

a guest
May 18th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.29 KB | None | 0 0
  1. // to compile this program you need to include hb15.zip
  2. // in the CLASSPATH environment variable
  3.  
  4. // We need to import the java.sql package to use JDBC
  5. import java.sql.*;
  6.  
  7. // for reading from the command line
  8. import java.io.*;
  9.  
  10. // for the login window
  11. import javax.swing.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14.  
  15. // for Format.printf() and Parameters.add()
  16. import com.braju.format.*;
  17.  
  18.  
  19. /*
  20. * This class implements a graphical login window and a simple text
  21. * interface for interacting with the branch table
  22. */
  23. public class branch implements ActionListener
  24. {
  25. // command line reader
  26. private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  27.  
  28. private Connection con;
  29.  
  30. // user is allowed 3 login attempts
  31. private int loginAttempts = 0;
  32.  
  33. // components of the login window
  34. private JTextField usernameField;
  35. private JPasswordField passwordField;
  36. private JFrame mainFrame;
  37.  
  38.  
  39. /*
  40. * constructs login window and loads JDBC driver
  41. */
  42. public branch()
  43. {
  44. mainFrame = new JFrame("User Login");
  45.  
  46. JLabel usernameLabel = new JLabel("Enter username: ");
  47. JLabel passwordLabel = new JLabel("Enter password: ");
  48.  
  49. usernameField = new JTextField(10);
  50. passwordField = new JPasswordField(10);
  51. passwordField.setEchoChar('*');
  52.  
  53. JButton loginButton = new JButton("Log In");
  54.  
  55. JPanel contentPane = new JPanel();
  56. mainFrame.setContentPane(contentPane);
  57.  
  58.  
  59. // layout components using the GridBag layout manager
  60.  
  61. GridBagLayout gb = new GridBagLayout();
  62. GridBagConstraints c = new GridBagConstraints();
  63.  
  64. contentPane.setLayout(gb);
  65. contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  66.  
  67. // place the username label
  68. c.gridwidth = GridBagConstraints.RELATIVE;
  69. c.insets = new Insets(10, 10, 5, 0);
  70. gb.setConstraints(usernameLabel, c);
  71. contentPane.add(usernameLabel);
  72.  
  73. // place the text field for the username
  74. c.gridwidth = GridBagConstraints.REMAINDER;
  75. c.insets = new Insets(10, 0, 5, 10);
  76. gb.setConstraints(usernameField, c);
  77. contentPane.add(usernameField);
  78.  
  79. // place password label
  80. c.gridwidth = GridBagConstraints.RELATIVE;
  81. c.insets = new Insets(0, 10, 10, 0);
  82. gb.setConstraints(passwordLabel, c);
  83. contentPane.add(passwordLabel);
  84.  
  85. // place the password field
  86. c.gridwidth = GridBagConstraints.REMAINDER;
  87. c.insets = new Insets(0, 0, 10, 10);
  88. gb.setConstraints(passwordField, c);
  89. contentPane.add(passwordField);
  90.  
  91. // place the login button
  92. c.gridwidth = GridBagConstraints.REMAINDER;
  93. c.insets = new Insets(5, 10, 10, 10);
  94. c.anchor = GridBagConstraints.CENTER;
  95. gb.setConstraints(loginButton, c);
  96. contentPane.add(loginButton);
  97.  
  98. // register password field and OK button with action event handler
  99. passwordField.addActionListener(this);
  100. loginButton.addActionListener(this);
  101.  
  102. // anonymous inner class for closing the window
  103. mainFrame.addWindowListener(new WindowAdapter()
  104. {
  105. public void windowClosing(WindowEvent e)
  106. {
  107. System.exit(0);
  108. }
  109. });
  110.  
  111. // size the window to obtain a best fit for the components
  112. mainFrame.pack();
  113.  
  114. // center the frame
  115. Dimension d = mainFrame.getToolkit().getScreenSize();
  116. Rectangle r = mainFrame.getBounds();
  117. mainFrame.setLocation( (d.width - r.width)/2, (d.height - r.height)/2 );
  118.  
  119. // make the window visible
  120. mainFrame.setVisible(true);
  121.  
  122. // place the cursor in the text field for the username
  123. usernameField.requestFocus();
  124.  
  125. try
  126. {
  127. // Load the Oracle JDBC driver
  128. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  129. }
  130. catch (SQLException ex)
  131. {
  132. System.out.println("Message: " + ex.getMessage());
  133. System.exit(-1);
  134. }
  135. }
  136.  
  137.  
  138. /*
  139. * connects to Oracle database named ug using user supplied username and password
  140. */
  141. private boolean connect(String username, String password)
  142. {
  143. // String connectURL = "jdbc:oracle:thin:@dbhost.ugrad.cs.ubc.ca:1521:ug";
  144. String connectURL = "jdbc:oracle:thin:@localhost:1521:ug";
  145.  
  146. try
  147. {
  148. con = DriverManager.getConnection(connectURL,username,password);
  149.  
  150. System.out.println("\nConnected to Oracle!");
  151. return true;
  152. }
  153. catch (SQLException ex)
  154. {
  155. System.out.println("Message: " + ex.getMessage());
  156. return false;
  157. }
  158. }
  159.  
  160.  
  161. /*
  162. * event handler for login window
  163. */
  164. public void actionPerformed(ActionEvent e)
  165. {
  166. if ( connect(usernameField.getText(), String.valueOf(passwordField.getPassword())) )
  167. {
  168. // if the username and password are valid,
  169. // remove the login window and display a text menu
  170. mainFrame.dispose();
  171. showMenu();
  172. }
  173. else
  174. {
  175. loginAttempts++;
  176.  
  177. if (loginAttempts >= 3)
  178. {
  179. mainFrame.dispose();
  180. System.exit(-1);
  181. }
  182. else
  183. {
  184. // clear the password
  185. passwordField.setText("");
  186. }
  187. }
  188.  
  189. }
  190.  
  191.  
  192. /*
  193. * displays simple text interface
  194. */
  195. private void showMenu()
  196. {
  197. int choice;
  198. boolean quit;
  199.  
  200. quit = false;
  201.  
  202. try
  203. {
  204. // disable auto commit mode
  205. con.setAutoCommit(false);
  206.  
  207. while (!quit)
  208. {
  209. System.out.print("\n\nPlease choose one of the following: \n");
  210. System.out.print("1. Insert Person\n");
  211. System.out.print("2. Delete Person\n");
  212. System.out.print("3. Update Person\n");
  213. System.out.print("4. showReservations\n");
  214. System.out.print("5. insertReservation\n");
  215. System.out.print("6. Quit\n>> ");
  216.  
  217. choice = Integer.parseInt(in.readLine());
  218.  
  219. System.out.println(" ");
  220.  
  221. switch(choice)
  222. {
  223. case 1: insertPerson(); break;
  224. case 2: deletePerson(); break;
  225. case 3: updatePerson(); break;
  226. case 4: showReservations(); break;
  227. case 5: insertReservation(); break;
  228. case 6: quit = true;
  229. }
  230. }
  231.  
  232. con.close();
  233. in.close();
  234. System.out.println("\nGood Bye!\n\n");
  235. System.exit(0);
  236. }
  237. catch (IOException e)
  238. {
  239. System.out.println("IOException!");
  240.  
  241. try
  242. {
  243. con.close();
  244. System.exit(-1);
  245. }
  246. catch (SQLException ex)
  247. {
  248. System.out.println("Message: " + ex.getMessage());
  249. }
  250. }
  251. catch (SQLException ex)
  252. {
  253. System.out.println("Message: " + ex.getMessage());
  254. }
  255. }
  256.  
  257.  
  258. private void insertPerson()
  259. {
  260.  
  261. PreparedStatement ps;
  262. /*PreparedStatement is an object that represents a precompiled SQL statement.
  263. * A SQL statement is precompiled and stored in "ps", which can be used to efficiently execute it couple times.
  264. * The methods setShort, setString for setting IN parameter values must specify types that are compatible with the defined SQL type of the input parameter.
  265. * I.e. if the IN parameter has SQL type INTEGER, then the method setInt should be used. If arbitrary parameter type conversions are required, the method
  266. * setObject should be used with a target SQL type.
  267. * In the following example of setting a parameter, con represents an active connection:
  268.  
  269. PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
  270. SET SALARY = ? WHERE ID = ?");
  271. pstmt.setBigDecimal(1, 153833.00)
  272. pstmt.setInt(2, 110592)
  273.  
  274.  
  275. */
  276.  
  277. try
  278. {
  279. ps = con.prepareStatement("INSERT INTO Person VALUES (?,?,?,?,?,?,?)");
  280.  
  281.  
  282. // System.out.print("\nCID: ");
  283. int CID = Integer.parseInt(in.readLine()); // Since this is Integer .
  284. ps.setInt(1, CID); // This must be ps.setInt
  285.  
  286. // System.out.print("\nName: ");
  287. String Name = in.readLine();
  288. ps.setString(2, Name);
  289.  
  290. // System.out.print("\nPhoneNum ");
  291. String PhoneNum = in.readLine();
  292. ps.setString(3, PhoneNum);
  293.  
  294. String DOB = in.readLine();
  295. ps.setString(4, DOB);
  296.  
  297. String Password = in.readLine();
  298. ps.setString(5, Password);
  299.  
  300. String Email = in.readLine();
  301. ps.setString(6, Email);
  302.  
  303. int isAdmin = Integer.parseInt( in.readLine());
  304. ps.setInt(6, isAdmin);
  305.  
  306. ps.executeUpdate(); // Executes ps = con.prepareStatement("INSERT INTO branch VALUES (?,?,?,?,?,?,?)");
  307. // Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.
  308.  
  309. // Returns:
  310. // either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
  311. // ps.ex
  312.  
  313. // commit work
  314. con.commit();
  315.  
  316. ps.close();
  317. }
  318. catch (IOException e)
  319. {
  320. System.out.println("IOException!");
  321. }
  322. catch (SQLException ex)
  323. {
  324. System.out.println("Message: " + ex.getMessage());
  325. try
  326. {
  327. // undo the insert
  328. con.rollback();
  329.  
  330. }
  331. catch (SQLException ex2)
  332. {
  333. System.out.println("Message: " + ex2.getMessage());
  334. System.exit(-1);
  335. }
  336. }
  337. }
  338.  
  339.  
  340. private void insertReservation()
  341. {
  342.  
  343. PreparedStatement psNewTicketForPerson;
  344. PreparedStatement psNewPaysFor;
  345. PreparedStatement psNewTransactTicket;
  346. PreparedStatement psNewTicket;
  347. PreparedStatement psNewTicketForSeat;
  348. PreparedStatement psUpdateSeat;
  349.  
  350. try
  351. {
  352. psNewTicketForPerson = con.prepareStatement("INSERT INTO TicketForPerson VALUES (?,?)");
  353. psNewTicketForPerson.setInt(1, Ticket_ID); // int
  354. psNewTicketForPerson.setInt(2, CID); // int
  355.  
  356. psNewPaysFor = con.prepareStatement("INSERT INTO PaysFor VALUES (?,?,?)");
  357. psNewPaysFor.setString(1, Payment_date); // String
  358. psNewPaysFor.setInt(2, Payment_ID); // int
  359. psNewPaysFor.setINt(3,CID); // int
  360.  
  361. psNewTransactTicket = con.prepareStatement("INSERT INTO TransactTicket VALUES (?,?)");
  362. psNewTransactTicket.setInt(1,Ticket_ID);
  363. psNewTransactTicket.setInt(2, Payment_ID);
  364.  
  365. psNewTicket = con.prepareStatement("INSERT INTO Ticket VALUES (?)");
  366. psNewTicket.setInt(1,Ticket_ID);
  367.  
  368. psNewTicketForSeat = con.prepareStatement("INSERT INTO TicketForSeat VALUES (?,?,?)");
  369. psNewTicketForSeat.setInt(1,Ticket_ID);
  370. psNewTicketForSeat.setInt(2,Seat_No);
  371. psNewTicketForSeat.setInt(3,Flight_No);
  372.  
  373.  
  374.  
  375. // System.out.print("\nName: ");
  376. String Name = in.readLine();
  377. ps.setString(2, Name);
  378.  
  379. // System.out.print("\nPhoneNum ");
  380. String PhoneNum = in.readLine();
  381. ps.setString(3, PhoneNum);
  382.  
  383. String DOB = in.readLine();
  384. ps.setString(4, DOB);
  385.  
  386. String Password = in.readLine();
  387. ps.setString(5, Password);
  388.  
  389. String Email = in.readLine();
  390. ps.setString(6, Email);
  391.  
  392. int isAdmin = Integer.parseInt( in.readLine());
  393. ps.setInt(6, isAdmin);
  394.  
  395. ps.executeUpdate(); // Executes ps = con.prepareStatement("INSERT INTO branch VALUES (?,?,?,?,?,?,?)");
  396.  
  397. // commit work
  398. con.commit();
  399.  
  400. ps.close();
  401. }
  402. catch (IOException e)
  403. {
  404. System.out.println("IOException!");
  405. }
  406. catch (SQLException ex)
  407. {
  408. System.out.println("Message: " + ex.getMessage());
  409. try
  410. {
  411. // undo the insert
  412. con.rollback();
  413.  
  414. }
  415. catch (SQLException ex2)
  416. {
  417. System.out.println("Message: " + ex2.getMessage());
  418. System.exit(-1);
  419. }
  420. }
  421. }
  422.  
  423.  
  424.  
  425.  
  426. /*
  427. * deletes a branch
  428. */
  429. private void deletePerson()
  430. {
  431. int bid;
  432. PreparedStatement ps;
  433.  
  434. try
  435. {
  436. ps = con.prepareStatement("DELETE FROM branch WHERE branch_id = ?");
  437.  
  438. System.out.print("\nBranch ID: ");
  439. bid = Integer.parseInt(in.readLine());
  440. ps.setInt(1, bid);
  441.  
  442. int rowCount = ps.executeUpdate();
  443.  
  444. if (rowCount == 0)
  445. {
  446. System.out.println("\nBranch " + bid + " does not exist!");
  447. }
  448.  
  449. con.commit();
  450.  
  451. ps.close();
  452. }
  453. catch (IOException e)
  454. {
  455. System.out.println("IOException!");
  456. }
  457. catch (SQLException ex)
  458. {
  459. System.out.println("Message: " + ex.getMessage());
  460.  
  461. try
  462. {
  463. con.rollback();
  464. }
  465. catch (SQLException ex2)
  466. {
  467. System.out.println("Message: " + ex2.getMessage());
  468. System.exit(-1);
  469. }
  470. }
  471. }
  472.  
  473.  
  474. /*
  475. * updates the name of a branch
  476. */
  477. private void updatePerson()
  478. {
  479. int bid;
  480. String bname;
  481. PreparedStatement ps;
  482.  
  483. try
  484. {
  485. ps = con.prepareStatement("UPDATE branch SET branch_name = ? WHERE branch_id = ?");
  486.  
  487. System.out.print("\nBranch ID: ");
  488. bid = Integer.parseInt(in.readLine());
  489. ps.setInt(2, bid);
  490.  
  491. System.out.print("\nBranch Name: ");
  492. bname = in.readLine();
  493. ps.setString(1, bname);
  494.  
  495. int rowCount = ps.executeUpdate();
  496. if (rowCount == 0)
  497. {
  498. System.out.println("\nBranch " + bid + " does not exist!");
  499. }
  500.  
  501. con.commit();
  502.  
  503. ps.close();
  504. }
  505. catch (IOException e)
  506. {
  507. System.out.println("IOException!");
  508. }
  509. catch (SQLException ex)
  510. {
  511. System.out.println("Message: " + ex.getMessage());
  512.  
  513. try
  514. {
  515. con.rollback();
  516. }
  517. catch (SQLException ex2)
  518. {
  519. System.out.println("Message: " + ex2.getMessage());
  520. System.exit(-1);
  521. }
  522. }
  523. }
  524.  
  525.  
  526. /*
  527. * display information about branches
  528. */
  529. private void showReservations()
  530. {
  531.  
  532. String FlightDepartsFrom__TimeStamp;
  533. String FlightArrivesAt__Timestamp;
  534. String FlightDepartsFrom__Airport_ID;
  535. String FlightArrivesAt__Airport_ID;
  536. int FlightDepartsFrom__Flight_No;
  537. int Seat__Seat_No;
  538. int Seat__IsBusinessClass;
  539. int Ticket__Ticket_ID;
  540. int PaysFor__Payment_ID;
  541. int Person__CID;
  542. String Person__Email;
  543. String Person__PhoneNum;
  544. String Person__DOB;
  545. String Person__Name;
  546.  
  547. Statement stmt;
  548. ResultSet rs;
  549.  
  550. try
  551. {
  552. stmt = con.createStatement();
  553. rs = stmt.executeQuery("SELECT FlightDepartsFrom.TimeStamp, FlightArrivesAt.Timestamp, FlightDepartsFrom.Airport_ID, FlightArrivesAt.Airport_ID, FlightDepartsFrom.Flight_No, Seat.Seat_No, Seat.IsBusinessClass, Ticket.Ticket_ID, PaysFor.Payment_ID, Person.CID, Person.Email, Person.PhoneNum, Person.DOB, Person.Name \" +
  554. "FROM Person, TicketForPerson, PaysFor, TransactTicket, Ticket, TicketForSeat, Seat, FlightDeparture, FlightArrival, Airport \" +
  555. "WHERE TicketForPerson.CID = Person.CID AND \" +
  556. "TicketForPerson.Ticket_ID = Ticket.Ticket_ID AND \" +
  557. "PaysFor.CID = Person.CID AND \" +
  558. "TransactTicket.Payment_ID = PaysFor.Payment_ID AND \" +
  559. "TransactTicket.Ticket_ID = Ticket.Ticket_ID AND \" +
  560. "TicketForSeat.Ticket_ID = Ticket.Ticket_ID AND \" +
  561. "TicketForSeat.Seat_no = Seat.Seat_no AND \" +
  562. "TicketForSeat.Flight_no = Seat.Flight_no AND \" +
  563. "Seat.Flight_no = FlightDeparture.Flight_no AND \" +
  564. "FlightDeparture.Tail_no = Plane.Tail_no AND \" +
  565. "FlightDeparture.Airport_ID = Airport.Airport_ID AND \" +
  566. "FlightArrival.Tail_no = Plane.Tail_no AND \" +
  567. "FlightArrival.Airport_ID = Airport.Airport_ID");
  568.  
  569. // get info on ResultSet
  570. ResultSetMetaData rsmd = rs.getMetaData();
  571.  
  572. // get number of columns
  573. int numFlightsBooked = rsmd.getColumnCount();
  574.  
  575. Parameters p = new Parameters();
  576.  
  577. // display column names;
  578. for (int i = 0; i < numCols; i++)
  579. {
  580. // get column name and print it
  581.  
  582. // The Format class provides the static printf() method
  583. // which behaves exactly like the printf() in
  584. // the C programming language. So for the line below
  585. // the text will be left aligned; it will also have a
  586. // minimum and maximum width of 15 characters.
  587. Format.printf("%-15.15s", p.add(rsmd.getColumnName(i+1)));
  588. }
  589.  
  590. System.out.println(" ");
  591.  
  592. while(rs.next())
  593. {
  594. // for display purposes get everything from Oracle
  595. // as a string
  596.  
  597. // simplified output formatting; truncation may occur
  598.  
  599. FlightDepartsFrom__TimeStamp = rs.getString("FlightDepartsFrom.TimeStamp");
  600. Format.printf("%-15.15s", p.add(bid));
  601.  
  602. FlightArrivesAt__TimeStamp = rs.getString("FlightArrivesAt.TimeStamp");
  603. Format.printf("%-15.15s", p.add(FlightArrivesAt__TimeStamp));
  604.  
  605. FlightDepartsFrom__Airport_ID = rs.getString("FlightDepartsFrom__Airport_ID");
  606. Format.printf("%-15.15s", p.add(FlightDepartsFrom__Airport_ID));
  607.  
  608. FlightArrivesAt__Airport_ID = rs.getString("FlightArrivesAt.Airport_ID");
  609. Format.printf("%-15.15s", p.add(FlightArrivesAt__Airport_ID));
  610.  
  611. FlightDepartsFrom__Flight_No = rs.getInteger("FlightDepartsFrom.Flight_No");
  612. Format.printf("%-15.15s", p.add(FlightDepartsFrom__Flight_No));
  613.  
  614. Seat__Seat_No = rs.getInteger("Seat.Seat_No");
  615. Format.printf("%-15.15s", p.add(Seat__Seat_No));
  616.  
  617. Seat__IsBusinessClass = rs.getInteger("Seat.IsBusinessClass");
  618. Format.printf("%-15.15s", p.add(Seat__IsBusinessClass));
  619.  
  620. Ticket__Ticket_ID = rs.getInteger("Ticket.Ticket_ID");
  621. Format.printf("%-15.15s", p.add(Ticket__Ticket_ID));
  622.  
  623. PaysFor__Payment_ID = rs.getInteger("PaysFor.Payment_ID");
  624. Format.printf("%-15.15s", p.add(PaysFor__Payment_ID));
  625.  
  626. Person__CID = rs.getInteger("Person.CID");
  627. Format.printf("%-15.15s", p.add(Person__CID));
  628.  
  629. Person__Email = rs.getString("Person.Email");
  630. Format.printf("%-15.15s", p.add(Person__Email));
  631.  
  632. Person__PhoneNum = rs.getString("Person.PhoneNum");
  633. Format.printf("%-15.15s", p.add(Person__PhoneNum));
  634.  
  635. Person__DOB = rs.getString("Person.DOB");
  636. Format.printf("%-15.15s", p.add(Person__DOB));
  637.  
  638. Person__Name = rs.getString("Person.Name");
  639. Format.printf("%-15.15s", p.add(Person__Name));
  640.  
  641.  
  642. // close the statement;
  643. // the ResultSet will also be closed
  644. stmt.close();
  645. }
  646. catch (SQLException ex)
  647. {
  648. System.out.println("Message: " + ex.getMessage());
  649. }
  650. }
  651.  
  652.  
  653. public static void main(String args[])
  654. {
  655. branch b = new branch();
  656. }
  657. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement