import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;

/**
 * @author jrao
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EmpDAOTest {
	   public static void main(String argv[])
	   {
	      // Create a new Dept record
	   	  EmpDAO empDAO = new EmpDAO();
	   	  
          // Set the attributes
          // All the setXXX(); and getXXX(); methods are generated by DB Cruiser.
	      empDAO.setEmpno(1234);
	      empDAO.setEname("John Smith");
	      empDAO.setJob("Manager");
	      empDAO.setMgr(5678);
	      empDAO.setHiredate(new java.sql.Date(Calendar.getInstance().getTime().getTime()));
	      empDAO.setSal(60000);
	      empDAO.setDeptno(30);
	      System.out.println(empDAO.statementOfInsert());

	      Connection conn = null;
	      Statement statement = null;
	      ResultSet resultSet = null;
	      try {
	          // Loca JDBC driver.
	          Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

	          // Get database connection.
	          //String connURL = "jdbc:oracle:thin:@localhost:1521:mathdb";
	          String connURL = "jdbc:oracle:thin:@192.168.1.100:1521:mathdb";
	          conn = DriverManager.getConnection (connURL, "scott", "tiger");

              // Insert the record into the database.
	          // public int insertDatabase(Connection conn); is generated by DB Cruiser.
	          empDAO.insertDatabase(conn); 
	          System.out.println("Inserted successfully");

	          // Get a record from the database.
	          String query ="select * from emp where empno=20";
	          statement = conn.createStatement();
	          resultSet = statement.executeQuery(query);
	          if (resultSet.next()) {
                  // Set attributes from the ResultSet.
			      // public void setAttributes(ResultSet rs); is generated by DB Cruiser.
	              empDAO.setAttributes(resultSet); 
	          }
	          System.out.println("The content of the empDAO:");
	          
              // public String toString(); method is generated by DB Cruiser.
	          System.out.println(empDAO.toString());
	          
              // Update the recard and write it into the database.
	          empDAO.setEname("David R.");
	          
              // public int updateDatabase(Connection conn); is generated by DB Cruiser.
			  // But you need to modify the WHERE condition in the SQL to fit your case
	          int count = empDAO.updateDatabase(conn); 
	          
	          System.out.println("Updated successfully");
	          System.out.println("The number of record updated = "+count);
	          System.out.println("The content of the empDAO:");
	          
              // public String toString(); method is generated by DB Cruiser.
	          System.out.println(empDAO.toString());

	      } catch(Exception e) {
	          e.printStackTrace();
	      } finally {
	          try {
	              if (resultSet != null) {
	                  resultSet.close();
	                  resultSet = null;
	              }
	              if (statement != null) {
	                  statement.close();
	                  statement = null;
	              }
	              if (conn != null) {
	                  conn.close();
	                  conn = null;
	              }
	          } catch(Exception e) {
	              e.printStackTrace();
	          }
	      }

	   }
}
