Java Test2

  1. personDAO
    • public class PersonDAO {
    •     Person person;
    •     Connection connection;
    •     ResultSet resultSet;
    •     final String user = "root";
    •     final String pwd = "mysql";
    •     final String url = "jdbc:mysql://localhost:3306/travel";
    •     final String query = "SELECT * FROM person ORDER By personid";
    •     public PersonDAO() {
    •         connection = null;
    •         resultSet = null;
    •         person = null;
    •         connectDB();
    •     }
    • private void connectDB() {
    • public void disconnectDB() {
    • public Person getFirstPerson() {
    • boolean isFirst() {
  2. isFirst()
    • Statement stmt = connection.createStatement();
    •                 resultSet = stmt.executeQuery(query); 

    • resultSet.first();
    •             person = new Person(resultSet.getString("name")
    •                     ,resultSet.getString("jobtitle")
    •                     ,resultSet.getString("frequentflyer"));

    • boolean isFirst() {
    •         try {
    •             return resultSet.isFirst();
    •         } catch (SQLException ex) {
    •             Logger.getLogger(PersonDAO.class.getName())
    •                     .log(Level.SEVERE, null, ex);
    •             return false;
    •         }
    •     }  // end isFirst()
  3. connect()
    package model;

    • import java.sql.Connection;
    • import java.sql.DriverManager;
    • import java.sql.ResultSet;
    • import java.sql.SQLException;
    • import java.sql.Statement;
    • import java.util.logging.Level;
    • import java.util.logging.Logger;

    • Connection connection;
    • ResultSet resultSet;
    • final String user = "root";

    • final String url = "jdbc:mysql://localhost:3306/travel";
    • final String query = "SELECT * FROM person ORDER By personid";

    • private void connectDB() {
    • try {
    •             connection = DriverManager.getConnection(url, user, pwd);
    • }
    • catch (SQLException ex1) {           Logger.getLogger(PersonDAO.class.getName())
    •                     .log(Level.SEVERE, null, ex1);
    • }
    •     } // end connectDB()
  4. disconnect()
    • public void disconnectDB() {
    • if (connection != null) {
    •     try {
    •                 connection.close();
    •          } catch (SQLException ex) {                Logger.getLogger(PersonDAO.class.getName())
    •                         .log(Level.SEVERE, null, ex);
    • }
    • } //end disconnectDB()
    •         }
  5. getFirstPerson()
    • public Person getFirstPerson() {
    • try {
    •      if (resultSet == null) {
    •      Statement stmt = 
    •       connection.createStatement();
    •      resultSet = stmt.executeQuery(query);
    •             }
    • resultSet.first();
    • person = new Person(resultSet.getString("name")
    •                     ,resultSet.getString("jobtitle")
    •                     ,resultSet.getString("frequentflyer"));
    •         } catch (SQLException ex) {
    •             Logger.getLogger(PersonDAO.class.getName())
    •                     .log(Level.SEVERE, null, ex);
    •         }
    •         return person;
    •     } //end getFirstPerson
  6. Person
    • package model;
    • public class Person {
    •     private String name;
    •     private String jobtitle;
    •     private String frequentflyer;

    •     public Person(String name, String jobtitle, String frequentflyer) {
    •         this.name = name;
    •         this.jobtitle = jobtitle;
    •         this.frequentflyer = frequentflyer;
    •     }

    •  public String getName() {
    •         return name;
    •     }
    • public void setName(String name) {
    •         this.name = name;
    •     }
    •     @Override
    •     public String toString() {
    •         return "Person{" + "name=" + name + ", jobtitle=" + jobtitle + ", frequentflyer=" + frequentflyer + '}';
    •     }
  7. Person, Hotel and Flight buttons
    • private void navButtons(java.awt.event.ActionEvent evt) {                            
    •         JButton btn = (JButton) evt.getSource();
    •         if (btn.getName()=="Person")
    •             jLabel1.setText("Personal Information");
    •         else if (btn.getName()=="Hotel")
    •             jLabel1.setText("Hotel Information");
    •         else
    •             jLabel1.setText("Flight Information");// TODO add your handling code here:
    •     }
  8. conditional operator ?
    Exp1 ? Exp2 : Exp3;
  9. The Finally Block
    • The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an
    • Exception.
    • Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
    • A finally block appears at the end of the catch blocks and has the following syntax −
  10. The Final Modifier
    • can be explicitly initialized only once.
    • can never be reassigned to refer to an
    • different object.
    • the data within the object can be changed. So, the state of the object can be changed but not the reference.
    • With variables, the final modifier often is used with static to make the constant a class variable.
  11. abstract class
    • An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.
    • A class cannot be both abstract and final (since a final class cannot be extended).
    • If a class contains abstract methods then the class should be declared abstract. Otherwise, a compile error will be thrown.
    • An abstract class may contain both abstract methods as well normal methods.
  12. Abstract Methods
    • An abstract method is a method declared without any implementation.
    • The methods body (implementation) is provided by the subclass.
    • Abstract methods can never be final or strict.
    • Any class that extends an abstract class must implement all the abstract methods of the super class, unless the subclass is also an
    • abstract class.
    • If a class contains one or more abstract methods, then the class must be declared abstract.
    • An abstract class does not need to contain
    • abstract methods.
    • The abstract method ends with a semicolon. Example: public abstract sample();
  13. The super keyword
    • is similar to this keyword.
    • used to differentiate the members of superclass from the members of subclass, if they have same names.
    • used to invoke the superclass constructor from subclass.
  14. class Dog extends Animal
    Animal a = new Animal(); // Animal reference and object
    Animal b = new Dog(); // Animal reference but Dog object
    a.move(); // runs the method in Animal class
    b.move(); // runs the method in Dog class
    b.bark();
    • In compile time, the check is made on the reference type.
    • However, in the runtime, JVM figures out the object type and would run the method
    • that belongs to that particular object.

    This program will throw a compile time error since b's reference type Animal doesn't have a method by the name of bark.
  15. Rules for Method Overriding
    • argument list should be exactly the same
    • The return type should be the same or a subtype
    • access level cannot be more restrictive
    • Instance methods can be overridden only if they are inherited by the subclass.
    • A method declared final cannot be overridden.
    • A method declared static cannot be overridden but can be re-declared.
    • If a method cannot be inherited, then it cannot be overridden.
    • A subclass within the same package as the instance's superclass
    • can override any superclass method that is not declared private or
    • final.
    • A subclass in a different package can only override the non-final methods declared public or protected.
    • Constructors cannot be overridden.
  16. public void move() {
    super.move(); // invokes the super class method
    System.out.println("Dogs can walk and run");

    Animal b = new Dog(); // Animal reference but Dog object
    b.move(); // runs the method in Dog class

    Output
    • Animals can move
    • Dogs can walk and run
  17. public interface Vegetarian{}
    public class Animal{}
    public class Deer extends Animal implements Vegetarian{}
    the Deer class is considered to be polymorphic since this has multiple inheritance.
  18. Java - Interfaces
    • An interface is a reference type in Java. It is similar to class. It is a
    • collection of abstract methods. A class implements an interface,
    • thereby inheriting the abstract methods of the interface.
    • Along with abstract methods, an interface may also contain constants,
    • default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
  19. Interfaces have the following properties −
    • An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
    • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
    • Methods in an interface are implicitly public.
Author
slc53
ID
330318
Card Set
Java Test2
Description
Java Test2
Updated