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
| public class MemberDAO { Connection con = null; String sql = ""; PreparedStatement pstmt = null; ResultSet rs = null;
private Connection getCon(){ final String DRIVER = "com.mysql.jdbc.Driver"; final String DBURL = "jdbc:mysql://localhost:3306/jspdb"; final String DBID = "root"; final String DBPW = "1234"; try { Class.forName(DRIVER); System.out.println("드라이버 로드 성공!"); con = DriverManager.getConnection(DBURL, DBID, DBPW); System.out.println("DB 연결 성공!" + con); } catch (ClassNotFoundException e) { System.out.println("드라이버 로드 실패!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("DB 연결 실패!"); e.printStackTrace(); } 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 insertMember(MemberBean mb){ try { con = getCon(); sql = "insert into itwill_member value(?,?,?,?,?,?,?)"; pstmt = con.prepareStatement(sql); pstmt.setString(1, mb.getId()); pstmt.setString(2, mb.getPw()); pstmt.setString(3, mb.getName()); pstmt.setInt(4, mb.getAge()); pstmt.setString(5, mb.getGender()); pstmt.setString(6, mb.getEmail()); pstmt.setTimestamp(7, mb.getReg_date()); pstmt.executeUpdate(); System.out.println("회원가입성공"); } catch (SQLException e) { System.out.println("회원가입실패"); e.printStackTrace(); } finally { closeDB(); } } }
|