Hi everyone
could anyone please tell me why I get notFound DuplicateException,notFoundException error. Thanks
Code:/* * CustomerDA.java * * Created on November 17, 2007, 1:11 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package project; import java.sql.*; // foe SQl import java.io.*; //needed for i/o import java.util.Vector; // for /** * * @author omer ali */ public class CustomerDA { static Customer aCustomer; // the data source is projectDatabase static String url = "jdbc:odbc:projectDatabase"; static Connection connection; static Statement statement; //declare variables for Address attribute values static String post_code; static int door_number; static int customer_num; static String phone_number; static String street_name; // Implement the tatic methods in Customer************* //initialize,terminate //establish the database conection public static void initialize() { try { //load the Jdbc _ odbc bridge driver for window Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //create connection instance connection= DriverManager.getConnection(url,"",""); // create statement object instance for this connection statement = connection.createStatement(); } catch (ClassNotFoundException e) { System.out.println(e); }catch (SQLException e) { System.out.println(e); } } // find an instant in the database public static Customer find(String key) throws NotFoudException { aCustomer = null; //define the SQL query satament using the post_code String sqlQuery = "SELECT door_number,street_name,post_code,phone_number " + "FROM customerTable " + "WHERE post_code = '" + key + " '"; // excute the SQl query statement try { ResultSet rs = statement.executeQuery(sqlQuery); //next method sets cursor & returns true if there is data boolean gotIt = rs.next(); if(gotIt) { //extract the data int customer_num = rs.getInt(1); int door_number = rs.getInt(2); String street_name = rs.getString(3); String phone_number = rs.getString(4); String post_code = rs.getString(5); // create customer instant aCustomer = new Customer(customer_num,door_number,street_name,post_code,phone_number); } else { //nothing was retrieved throw (new NotFoundException("Not Foun")); } rs.close(); } catch (SQLException e) { System.out.println(e); } return aCustomer; } //get All instant from database public static Vector getAll() { Vector customers = new Vector(); //define SQL query staement for all String sqlQuery = "SELECT door_number,street_name,post_code,phone_number" + " FROM customerTable"; try { //excute the SQL query staement ResultSet rs = statement.executeQuery(sqlQuery); boolean moreData = rs.next(); while(moreData) { //extract the data int customer_num = rs.getInt(1); int door_number = rs.getInt(2); String street_name = rs.getString(3); String phone_number = rs.getString(4); String post_code = rs.getString(5); // create customer instant aCustomer = new Customer(customer_num,door_number,street_name,post_code,phone_number); customers.addElement(aCustomer); moreData= rs.next(); } rs.close(); } catch(SQLException e) { System.out.println(e); } return customers; } // close database connection public static void terminate() { try { //close everything statement.close(); connection.close(); } catch (SQLException e) { System.out.println(e); } } // implement the instant method in Customer******* //addNew, upDate // add new instantto database public static void addNew(Customer aCustomer) throws DuplicateFormatFlagsException { //retrieve the customer atttribute values door_number = aCustomer.getDoor_number(); street_name = aCustomer.getStreet_name(); post_code = aCustomer.getPost_code(); phone_number = aCustomer.getPhone_number(); /** create the SQL insert statement using attribute values */ String sqlInsert = "INSERT INTO customerTable " + "(door_number,street_name,post_code,phone_number)" + "VALUES ('" + door_number + "', '" + street_name + "' , '" + post_code + "' , '" + phone_number + "')"; //see if the customer already exist try { Customer c = find(post_code); throw (new DuplicateException("Customer alreadt exist")); } // if NotFoundException add Customer to database catch (NotFoundReasonHelper e) { try { //excute the upDate statement int result = statement.executeUpdate(sqlInsert); } catch (SQLException ee) { System.out.println(ee); } } } //upDate instance in the database public static void update(Customer aCustomer) throws NotFoundException { //retrieve the customer atttribute values door_number = aCustomer.getDoor_number(); street_name = aCustomer.getStreet_name(); post_code = aCustomer.getPost_code(); phone_number = aCustomer.getPhone_number(); // define the sql query staement using the post Code key String sqlUpdate = "UPDATE customerTable " + "SET door_number = '" + door_number + "' , " + "strrt_name = '"+ street_name + " ' , " + "post_code = '" + post_code + " ' , " + " WHERE phone_number = '" + phone_number + " ' "; try { Customer c = find (post_code); // if found it excute the upDate statement, a 1 return is good delete int result = statement.executeUpdate(sqlUpdate); } catch (SQLException ee) { System.out.println(ee); } } }




Reply With Quote