-
day50 - JSP(model2, login, DBconnection)KIC/JSP 2021. 8. 26. 03:45반응형
[MemberController.java]
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.MemberDao; import model.Member; public class MemberController extends Action{ // /kicPro/member/hello public String hello(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("hello", "hello 테스트 입니다."); return "/view/hello.jsp"; } // /kicPro/member/list public String list(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("list", "list 테스트 입니다."); return "/view/list.jsp"; } // /kicPro/member/memberInput public String memberInput(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("input", "member Input 테스트 입니다."); return "/view/member/memberInput.jsp"; } public String memberInputPro(HttpServletRequest request, HttpServletResponse response) throws Exception { MemberDao dao = new MemberDao(); Member m = new Member(); m.setId(request.getParameter("id")); m.setPass(request.getParameter("pass")); m.setName(request.getParameter("name")); m.setGender(Integer.parseInt(request.getParameter("gender"))); m.setTel(request.getParameter("tel")); m.setEmail(request.getParameter("email")); m.setPicture(""); dao.memberInsert(m); request.setAttribute("m", m); return "/view/member/list.jsp"; } }
[BoarderController.java]
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BoardController extends Action{ public String hello(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("hello", "hello 테스트 입니다."); return "/view/hello.jsp"; } public String list(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("list", "list 테스트 입니다."); return "/view/list.jsp"; } public String memberInput(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setAttribute("input", "member Input 테스트 입니다."); return "/view/memberInput.jsp"; } }
[DBConnection]
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConnection { public static Connection getConnection() { Connection con=null; try { Class.forName("oracle.---.---"); String url="jdbc:oracle:thin:@localhost:----"; String userId = "---"; String passwd = "---"; con = DriverManager.getConnection(url, userId, passwd); } catch (Exception e) { e.printStackTrace(); } return con; } public static void close(Connection con, Statement stmt, ResultSet rs ) { try { if(rs !=null) rs.close(); if(stmt !=null) stmt.close(); if(con !=null) { con.commit(); con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
[memberDao.java]
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import model.Member; import util.DBConnection; public class MemberDao { public int memberInsert(Member member) { Connection con = DBConnection.getConnection(); PreparedStatement pstmt = null; String sql = "insert into book values (?, ?, ?, ?, ?, ?, ?)"; try { pstmt = con.prepareStatement(sql); pstmt.setString(1, member.getId()); pstmt.setString(2, member.getPass()); pstmt.setString(3, member.getName()); pstmt.setInt(3, member.getGender()); pstmt.setString(3, member.getTel()); pstmt.setString(3, member.getEmail()); pstmt.setString(3, member.getPicture()); return pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBConnection.close(con, pstmt, null); } return 0; } public List<Member> memberList() { Connection con = DBConnection.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from member"; List<Member> li = new ArrayList<Member>(); try { pstmt = con.prepareStatement(sql); rs=pstmt.executeQuery(); while(rs.next()) { Member b = new Member(); li.add(b); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBConnection.close(con, pstmt, rs); } return li; } }
300x250'KIC > JSP' 카테고리의 다른 글
day52 - JSP(login, memberList) (0) 2021.08.27 day51 - JSP(model2, login) (0) 2021.08.26 day49 - JSP(model2) (0) 2021.08.25 day48 - JSP(model2) (0) 2021.08.24 day47 - JSP(AJAX, JSON, jQuery) (0) 2021.08.20