2014-03-06 85 views
-1

我正在處理我的項目,而當我這樣做時,我在項目中遇到了一些錯誤。我正在創建一個數據訪問對象,這裏是我正在處理的方法列表。使用JDBC編程時出錯

public class OrderDAO { 

    public void add(OrderInfo o){ 
     Connection con = null; 
     PreparedStatement ps = null; 
     String q = "insert into OrderInfo values (oseq.nextval, ?, ?, ?)"; 

     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 

      ps.setInt(1, o.getCnum()); 
      ps.setInt(2, o.getPnum()); 
      ps.setInt(3, o.getQuant()); 
      ps.executeUpdate(); 
      System.out.println("OrderInfo Registration Success"); 
     } catch (SQLException e) { 
      System.out.println("OrderInfo Registration fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 
    } 
    public ArrayList<OrderInfo> search(){ 
     Connection con = null; 
     Statement st = null; 
     ResultSet rs = null; 
     ArrayList<OrderInfo> orderInfoList = new ArrayList<>(); 

     String q = "Select onum, cnum, pnum, quant" 
       + " from OrderInfo"; 
     try { 
      con = DBUtil.getConnection(); 
      st = con.createStatement(); 
      rs = st.executeQuery(q); 
      while (rs.next()) { 
       OrderInfo orderInfo = new OrderInfo(rs.getInt(1), rs.getInt(2), 
         rs.getInt(3), rs.getInt(4)); 
       orderInfoList.add(orderInfo); 
      } // end of while 

     } catch (SQLException e) { 
      System.out.println("Failed to search"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(rs); 
      DBUtil.close(st); 
      DBUtil.close(con); 
     } 
     return orderInfoList; 
    } 
    public OrderInfo search(int onum){ 
     OrderInfo orderInfo = null; 
     Connection con = null; 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     String q = "Select onum, cnum, pnum, quant " 
       + " from OrderInfo where onum = ?"; 
     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 

      ps.setInt(1, onum); 
      rs = ps.executeQuery(); 
      if (rs.next()) { 
       orderInfo = new OrderInfo(rs.getInt(1), rs.getInt(2), rs.getInt(3), 
         rs.getInt(4)); 
      } 

     } catch (SQLException e) { 
      System.out.println("Search Fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(rs); 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 
     return orderInfo; 
    } 
    public DeliveryInfo deliverySearch(int onum){ 
     Connection con = null; 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     DeliveryInfo di = null; 
     String q = "select onum, cnum, cname, caddress, quant " 
       + " from orderinfo o, customer c " 
       + " where o.onum = c.cnum and onum = ?"; 
     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 
      ps.setInt(1, onum); 
      rs = ps.executeQuery(); 
      if (rs.next()) { 
       di = new DeliveryInfo(); 
       di.setOnum(rs.getInt("onum")); 
       di.setCnum(rs.getInt("cnum")); 
       di.setCname(rs.getString("cname")); 
       di.setCaddress(rs.getString("caddress")); 
       di.setQuant(rs.getInt("quant")); 
      } 
     } catch (SQLException e) { 
      System.out.println("Search Fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(rs); 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 
     return di; 
    } 
    public ArrayList<DeliveryInfo> deliverySearch(){ 
     Connection con = null; 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     DeliveryInfo di = null; 
     ArrayList<DeliveryInfo> deliveryInfoList = new ArrayList<>(); 
     String q = "Select onum, cnum, pnum, quant, cname, caddress" 
       + " from OrderInfo o , Customer c" 
       + " where o.onum = c.cnum and onum = ?"; 
     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 
      rs = ps.executeQuery(); 

      while(rs.next()){ 
       di = new DeliveryInfo(rs.getInt(1),rs.getInt(2),rs.getInt(3), rs.getInt(4), rs.getString(5), rs.getString(6)); 
       deliveryInfoList.add(di); 
      } // end of while 

     } catch (SQLException e) { 
      System.out.println("Search Fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(rs); 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 
     return deliveryInfoList; 
    } 
    public int getOrderPrice(int onum) { 
     int tprice = 0; 
     Connection conn = null; 
     PreparedStatement preSt = null; 
     ResultSet resultSet = null; 

     try { 
      conn = DBUtil.getConnection(); 
      String query = "select quant, price from orderinfo, product " 
        + " where orderinfo.pnum=product.pnum and onum = ?"; 
      preSt = conn.prepareStatement(query); 
      preSt.setInt(1, onum); 

      resultSet = preSt.executeQuery(); 
      if (resultSet.next()) { 
       int quant = resultSet.getInt("quant"); 
       int price = resultSet.getInt("price"); 
       tprice = quant * price; 
      } 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(resultSet); 
      DBUtil.close(preSt); 
      DBUtil.close(conn); 
     } 
     return tprice; 
    }// end of getOrderPrice 
} 

當我測試這個DAO的時候,我不斷收到有事可做與標識符的錯誤消息...

我還是新來的Java和編程..所以我將非常感謝您的幫助。

+3

如果您發佈確切的錯誤消息,這將是最有幫助的。 –

+0

錯誤信息以某種方式彈出在控制檯上的韓語...怪異的呃?與標識符有關...... – user3387844

+0

你在做什麼來「測試這個DAO」,並且正如Brian評論的那樣,錯誤消息對於調試這個(即使它是韓文版本)是必不可少的。 – user3360944

回答

0
first you should have created a table. Here's an example of creating a table. 

    public class TableCreate { 
    // Create Data (save) Create table 
     public static void main(String[] args) throws ClassNotFoundException, 
     SQLException { 
      // 1. Create Driver (Object will be created automatically; register onto DriverManager 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      // 2. Create Connection 
      Connection con = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:XE", "doll", "doll"); 
      // 3. Query, create Query performing object 
      String q = "create table employee (snum int primary key, " 
        + "sname varchar2(30), sdate varchar2(30), spart varchar2(20))"; 
      Statement st = con.createStatement(); 
      // 4. Perform Query 
      boolean rs = st.execute(q); //all - return: true - if ResultSet comes, then true/ false - if else 
      //ResultSet rs = st.executeQuery (q); //Select 
      //int rs = st.executeUpdate(q); //insert, update, delete 
      // 5. Result Processing 
      String q2 = "create sequence sseq"; 
      st.execute(q2);  
      // 6. Return Resource (close backwards) 
      st.close(); 
      con.close(); 
     } 
    } 

*** 

Next, you will need these Utilities (Classes): 

    public class DBUtil { 

     static { 
      // Create Driver 
      try { 
       Class.forName("oracle.jdbc.driver.OracleDriver"); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     public static Connection getConnection() throws SQLException { 
      // Get Connection 
      Connection con = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:XE", "scsa", "scsa"); 
      return con; 
     } 

     public static void close(ResultSet rs) { 
      if (rs != null) { 
       try { 
        rs.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
     public static void close(Statement ps) { 
      if (ps != null) 
       try { 
        ps.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
     public static void close(Connection con) { 
      if (con != null) 
       try { 
        con.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
    } //end of DBUtil 

*** 

more utilities: 

    public class DBUtil { 

     static { 
      // create Driver 
      try { 
       Class.forName("oracle.jdbc.driver.OracleDriver"); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     public static Connection getConnection() throws SQLException { 
      // Connection creation 
      Connection con = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:XE", "scsa", "scsa"); 
      return con; 
     } 

     public static void close(ResultSet rs) { 
      if (rs != null) { 
       try { 
        rs.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
     public static void close(Statement ps) { 
      if (ps != null) 
       try { 
        ps.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
     public static void close(Connection con) { 
      if (con != null) 
       try { 
        con.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } 
    } //end of DBUtil 

*** 
More Utilites 

    public class RecordNotFoundException extends Exception { 

     public RecordNotFoundException(){ 
      super("Such record does not exist."); 
     } 
    } 

*** 
add fnct: 

    public void add(OrderInfo o){ 
      Connection con = null; 
      PreparedStatement ps = null; 
      String q = "insert into OrderInfo values (oseq.nextval, ?, ?, ?)"; 

      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 

       ps.setInt(1, o.getCnum()); 
       ps.setInt(2, o.getPnum()); 
       ps.setInt(3, o.getQuant()); 
       ps.executeUpdate(); 
       System.out.println("Register Complete"); 
      } catch (SQLException e) { 
       System.out.println("Register Fail"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
     } 

    //////////////////////////////////////////////////// 

    public void insertBook(Book book) throws DuplicateException { 
      if (isbnExists(book.getIsbn())) { 
       throw new DuplicateException(); 
      } 

      Connection con = null; 
      String q = "Insert into Book values(?, ?, ?, ?, ?, ?)"; 
      PreparedStatement ps = null; 
      try { 
       con = DBUtil.getConnection(); 
       // Statement st = con.createStatement(); //Cannot recognize question marks. so don't use 
       ps = con.prepareStatement(q); 
       ps.setString(1, book.getIsbn()); 
       ps.setString(2, book.getTitle()); 
       ps.setString(3, book.getAuthor()); 
       ps.setInt(4, book.getPrice()); 
       ps.setString(5, book.getPublisher()); 
       ps.setString(6, book.getDescription()); 

       ps.executeUpdate(); 
       System.out.println("1 Book Registration Complete"); 
      } catch (SQLException e) { 
       System.out.println("Failed to save data"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
     } 

*** 
ArrayList<Class> search() fnct: 

    public ArrayList<Customer> search(){ 
      Connection con = null; 
      Statement st = null; 
      ResultSet rs = null; 
      ArrayList<Customer> customerList = new ArrayList<>(); 

      String q = "Select cnum, cname, caddress from Customer2"; 
      try{ 
       con = DBUtil.getConnection(); 
       st = con.createStatement(); 
       rs = st.executeQuery(q); 
       while(rs.next()){ 
        Customer customer = new Customer(rs.getInt(1), rs.getString(2), 
          rs.getString(3)); 
        customerList.add(customer); 
       } // end of while 

      } catch (SQLException e) { 
       System.out.println("Failed to search"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(st); 
       DBUtil.close(con); 
      } 
      return customerList; 
     } 
    } 

*** 
ArrayList<subClass> subSearch() : 

    public ArrayList<DeliveryInfo> deliverySearch(){ 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      DeliveryInfo di = null; 
      ArrayList<DeliveryInfo> deliveryInfoList = new ArrayList<>(); 
      String q = "Select onum, cnum, pnum, quant, cname, caddress" 
        + " from OrderInfo o , Customer c" 
        + " where o.onum = c.cnum and onum = ?"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       rs = ps.executeQuery(); 

       while(rs.next()){ 
        di = new DeliveryInfo(rs.getInt(1),rs.getInt(2),rs.getInt(3), rs.getInt(4), rs.getString(5), rs.getString(6)); 
        deliveryInfoList.add(di); 
       } // end of while 

      } catch (SQLException e) { 
       System.out.println("Failed to Search"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return deliveryInfoList; 
     } 

*** 
public ArrayList<class> search(String name): 

    public ArrayList<DeliveryInfo> deliverySearch(){ 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      DeliveryInfo di = null; 
      ArrayList<DeliveryInfo> deliveryInfoList = new ArrayList<>(); 
      String q = "Select onum, cnum, pnum, quant, cname, caddress" 
        + " from OrderInfo o , Customer c" 
        + " where o.onum = c.cnum and onum = ?"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       rs = ps.executeQuery(); 

       while(rs.next()){ 
        di = new DeliveryInfo(rs.getInt(1),rs.getInt(2),rs.getInt(3), rs.getInt(4), rs.getString(5), rs.getString(6)); 
        deliveryInfoList.add(di); 
       } // end of while 

      } catch (SQLException e) { 
       System.out.println("Failed to search"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return deliveryInfoList; 
     } 

*** 

public className search(int num): 

    public DeliveryInfo deliverySearch(int onum){ 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      DeliveryInfo di = null; 
      String q = "select onum, cnum, cname, caddress, quant " 
        + " from orderinfo o, customer c " 
        + " where o.onum = c.cnum and onum = ?"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       ps.setInt(1, onum); 
       rs = ps.executeQuery(); 
       if (rs.next()) { 
        di = new DeliveryInfo(); 
        di.setOnum(rs.getInt("onum")); 
        di.setCnum(rs.getInt("cnum")); 
        di.setCname(rs.getString("cname")); 
        di.setCaddress(rs.getString("caddress")); 
        di.setQuant(rs.getInt("quant")); 
       } 
      } catch (SQLException e) { 
       System.out.println("failed to search"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return di; 
     } 

*** 

public int getOrderPrice(int num) 

    public DeliveryInfo deliverySearch(int onum){ 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      DeliveryInfo di = null; 
      String q = "select onum, cnum, cname, caddress, quant " 
        + " from orderinfo o, customer c " 
        + " where o.onum = c.cnum and onum = ?"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       ps.setInt(1, onum); 
       rs = ps.executeQuery(); 
       if (rs.next()) { 
        di = new DeliveryInfo(); 
        di.setOnum(rs.getInt("onum")); 
        di.setCnum(rs.getInt("cnum")); 
        di.setCname(rs.getString("cname")); 
        di.setCaddress(rs.getString("caddress")); 
        di.setQuant(rs.getInt("quant")); 
       } 
      } catch (SQLException e) { 
       System.out.println("failed to search"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return di; 
     } 

*** 

public void update(int anum, int balance) 

    public void update(int anum, int balance) throws RecordNotFoundException { 
    //  if (search(anum) == null) { 
    //   throw new RecordNotFoundException(); 
    //  } 
    // 
    //  boolean result = false; 
      Connection con = null; 
      PreparedStatement ps = null; 
      String q = "update Account set balance = ? where anum = ?"; 

      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       ps.setInt(1, balance); 
       ps.setInt(2, anum); 

       ps.executeUpdate(); 
      } catch (SQLException e) { 
       System.out.println("update fail"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
     } 

*** 
pub boolean update(int snum, String spart): 

public boolean update(int snum, String spart) throws RecordNotFoundException { 
     if(search(snum) == null){ 
      throw new RecordNotFoundException(); 
     } 


     boolean result = false; 
     Connection con = null; 
     PreparedStatement ps = null; 
     String q = "update Employee set spart = ? where snum = ?"; 

     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 
      ps.setString(1, spart); 
      ps.setInt(2, snum); 

      int re = ps.executeUpdate(); 
      if (re > 0) { 
       result = true; 
      } 
      System.out.println("update success"); 
     } catch (SQLException e) { 
      System.out.println("update fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 
     return result; 
    } 

*** 
pub void update(Class c): 

public void noticeUpdate(Notice n) throws RecordNotFoundException{ 
     if (!numExists(n.getNum())) { 
      throw new RecordNotFoundException(); 
     } 

     Connection con = null; 
     PreparedStatement ps = null; 
     String q = "update Notice set writer = ?, title = ?, cont = ? where num = ?"; 

     try { 
      con = DBUtil.getConnection(); 
      ps = con.prepareStatement(q); 
      ps.setString(1, n.getWriter()); 
      ps.setString(2, n.getTitle()); 
      ps.setString(3, n.getCont()); 
      ps.setInt(4, n.getNum()); 

      ps.executeUpdate(); 
      System.out.println("update success"); 
     } catch (SQLException e) { 
      System.out.println("update fail"); 
      e.printStackTrace(); 
     } finally { 
      DBUtil.close(ps); 
      DBUtil.close(con); 
     } 

    } 

*** 
OrderTest{ : 

public class OrderTest { 

    public static void main(String[] args) { 


     OrderDAO adao = new OrderDAO(); 
//  adao.add(new OrderInfo(1,10,11000)); 
//  adao.add(new OrderInfo(2,11,220000)); 
//  adao.add(new OrderInfo(3,12,330000)); 
//  System.out.println(OrderDAO.search()); 
//  System.out.println(OrderDAO.search(1)); 
//  System.out.println(OrderDAO.getOrderPrice(2)); 


    } 

    public static void displayAll(ArrayList<OrderInfo> list){ 
     System.out.println("============================"); 
     for(OrderInfo o : list){ 
      System.out.println(o); 
     } 
    } 
} 

*** 
ProductTest{ 

    public class ProductTest { 
    static ProductDAO dao; 

     public static void main(String[] args) throws ClassNotFoundException { 
      dao = new ProductDAO(); 
    //  dao.add(new Product (10, "Galaxy Note 3", 140)); 
    //  dao.add(new Product (11, "Galaxy S4", 140)); 
    //  dao.add(new Product (12, "SmartTv", 7000)); 
      dao.add(new Product (14, "PS3", 7000, "Sony")); 


    //  displayAll(); 

      ArrayList<Product> plist = dao.search(); 
      displayAll(plist); 


     } 

     public static void displayAll(ArrayList<Product> plist){ 
      System.out.println("=======Product List======="); 
      for(Product p: plist){ 
       System.out.println(p); 
      } 
     } 
    } 

    *** 

    Test w/ columnNames: 

     public class EmployeeTest { 
      static EmployeeDao dao; 

      public static void main(String[] args) throws ClassNotFoundException, SQLException { 
       dao = new EmployeeDao(); 
     //  dao.delete(6); 
     //  dao.add("John", "RD"); 
     //  dao.add("Michael", "Development"); 
     //  dao.add("Hank", "Maintenance"); 
     //  dao.add("Andy", "Engineering"); 
     //  dao.add("Chris", "Global sales"); 
     //  dao.update(11, "Finance"); 

       ArrayList<Employee> list = dao.search(); 
       System.out.println(dao.getColumnNames()); 
       displayAll(list); 

      } 


     public static void displayAll(ArrayList<Employee> list) throws ClassNotFoundException, SQLException{ 
      System.out.println("================== Employee List =================="); 
      for(Employee emp : list){ 
       System.out.println(emp); 
      } 
     } 

    } 


*** 

pub CharSequence getColumnNames(){: 

    public CharSequence getColumnNames() { 

      StringBuilder sb = new StringBuilder(); 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      String q = "Select * from employee"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 

       rs = ps.executeQuery(); 
       ResultSetMetaData md = rs.getMetaData(); 
       int count = md.getColumnCount(); 
       for (int i = 1; i <= count; i++) { 
        sb.append(md.getColumnName(i)); 
        sb.append(" | "); 
       } 
      } catch (SQLException e) { 
       System.out.println("Search Failed"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 

      return sb; 
     } 

    /////////////////////////////////////////////////////// 

    public String getHeader(){ 
      StringBuilder sb = new StringBuilder(); 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      String q = "Select * " + " from Notice"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 

       rs = ps.executeQuery(); 
       ResultSetMetaData md = rs.getMetaData(); 
       int count = md.getColumnCount(); 
       for (int i = 1; i <= count; i++) { 
        sb.append(md.getColumnName(i)); 
        sb.append(" | "); 
       } 
      } catch (SQLException e) { 
       System.out.println("검색 실패"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 

      return sb.toString(); 
     } 




*** 
pub boolean delete (int snum): 

    public boolean delete(int snum) { 
      boolean result = false; 
      // Product p = search(num); 
      Connection con = null; 
      PreparedStatement ps = null; 
      String q = "delete from Employee where snum = ?"; 

      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       ps.setInt(1, snum); 

       int re = ps.executeUpdate(); 
       if (re > 0) { 
        result = true; 
       } 
       System.out.println("Delete Success"); 
      } catch (SQLException e) { 
       System.out.println("Delete Fail"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return result; 

     } 

*** 

pub void delete(int num) : 

    public void delete(int num) { 
      Connection con = null; 
      PreparedStatement ps = null; 
      String q = "delete from Product where num = ?"; 

      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 
       ps.setInt(1, num); 

       ps.executeUpdate(); 
       System.out.println("Delete Success"); 
      } catch (SQLException e) { 
       System.out.println("Delete Fail"); 
       e.printStackTrace(); 
      }finally{ 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 

     } 


*** 

tableCreate : 

    public class TableCreate { 
    // 데이터 생성 (저장) 테이블 생성 
     public static void main(String[] args) throws ClassNotFoundException, 
     SQLException { 
      // 1. Create Driver 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      // 2. Create Connection 
      Connection con = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:XE", "scsa", "scsa"); 
      // 3. Query, create Query Performing Object 
      String q = "create table employee (snum int primary key, " 
        + "sname varchar2(30), sdate varchar2(30), spart varchar2(20))"; 
      Statement st = con.createStatement(); 
      // 4. Query Perform 
      boolean rs = st.execute(q); //all - return: true - ResultSet== true/ false - else 
      //ResultSet rs = st.executeQuery (q); //Select 
      //int rs = st.executeUpdate(q); //insert, update, delete 
      // 5. 결과처리 
      String q2 = "create sequence sseq"; 
      st.execute(q2);  
    //  if (rs>0) { 
    //   System.out.println("Delete Complete"); 
    //  } 
      // 6. Return resources (backwards close) 
      st.close(); 
      con.close(); 
     } 
    } 


*** 

    pub boolean numExist(int num) : 

    public boolean numExists(int num){ 
      boolean flag = false; 
      Connection con = null; 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      String q = "Select num from Notice where num = ?"; 
      try { 
       con = DBUtil.getConnection(); 
       ps = con.prepareStatement(q); 

       ps.setInt(1, num); 
       rs = ps.executeQuery(); 
       if (rs.next()) { 
        flag = true; 
       } 
      } catch (SQLException e) { 
       System.out.println("Search Fail"); 
       e.printStackTrace(); 
      } finally { 
       DBUtil.close(rs); 
       DBUtil.close(ps); 
       DBUtil.close(con); 
      } 
      return flag; 
     } 
+0

這不是OP要求的。我認爲OP已經創建了一個表,'DBUtil'是他使用的一個現有類。所以這真的是他得到的錯誤信息。 – tonga

相關問題