Example #1
0
  public int insert(BookVo vo) {
    int count = 0;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      // 1. 드라이버 로딩
      Class.forName("oracle.jdbc.driver.OracleDriver");

      // 2. 연결 얻어오기
      String url = "jdbc:oracle:thin:@localhost:1521:xe";
      conn = DriverManager.getConnection(url, "skudb", "skudb");

      // 3. statement 준비
      String sql = "insert into book values(seq_book.nextval, ?, ?, ?, ?)";
      pstmt = conn.prepareStatement(sql);

      // 4. 바인딩
      pstmt.setString(1, vo.getTitle());
      pstmt.setInt(2, vo.getRate());
      pstmt.setInt(3, vo.getStatus());
      pstmt.setLong(4, vo.getAuthorNo());

      // 5. query 실행
      count = pstmt.executeUpdate();

    } catch (ClassNotFoundException e) {
      System.out.println("드라이버 로딩 실패 :" + e);
    } catch (SQLException e) {
      System.out.println("error:" + e);
    } finally {
      try {
        // 6. 자원정리
        if (pstmt != null) {
          pstmt.close();
        }

        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        System.out.println("error:" + e);
      }
    }

    return count;
  }