1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| Connection con = null; String sql = ""; PreparedStatement pstmt = null; ResultSet rs = null;
private Connection getCon() throws Exception { Context init = new InitialContext(); DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/webpro"); con = ds.getConnection(); System.out.println("디비연결성공"); return con; }
public void closeDB(){ try{ if(rs != null) rs.close(); if(pstmt != null) pstmt.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } }
public void insertBoard(BoardBean bb){ int num = 0; try { con = getCon();
sql = "select max(bno) from fun_board";
pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery();
if(rs.next()){ num = rs.getInt(1)+1; }
System.out.println("저장될 글번호 : "+num);
sql = "insert into fun_board (bno,name,pw,subject,content," + "readcount,re_ref,re_lev,re_seq,date,ip,file) " + "values(?,?,?,?,?,?,?,?,?,now(),?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num); pstmt.setString(2, bb.getName()); pstmt.setString(3, bb.getPw()); pstmt.setString(4, bb.getSubject()); pstmt.setString(5, bb.getContent()); pstmt.setInt(6, 0); pstmt.setInt(7, num); pstmt.setInt(8, 0); pstmt.setInt(9, 0); pstmt.setString(10, bb.getIp()); pstmt.setString(11, bb.getFile());
pstmt.executeUpdate();
System.out.println(num+"번 글쓰기 완료!");
} catch (Exception e) { e.printStackTrace(); }finally { closeDB(); } }
|