JSP CRUD Example jsp 기본예제

2018.07.28 15:06

졸리운_곰 조회 수:166

JSP CRUD Example jsp 기본예제

JSP CRUD Example

We can easily create CRUD Example in JSP. Here, we are using DAO files for database and JSTL for traversing records.

Download jstl.jar and mysql-connector.jar

Download jstl1.2.jar file
Download mysql-connector.jar

 

jspcrudapp.zip


Download SQL File to Import in MySQL

Download SQL File


Download Project

download CRUD project in JSP


CRUD Example

Directory Structure in Eclipse

JSP CRUD Example
index.jsp

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>JSP CRUD Example</title>  
  6. </head>  
  7. <body>  
  8. <h1>JSP CRUD Example</h1>  
  9. <a href="adduserform.jsp">Add User</a>  
  10. <a href="viewusers.jsp">View Users</a>  
  11.   
  12. </body>  
  13. </html>  

adduserform.jsp

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Add User Form</title>  
  6. </head>  
  7. <body>  
  8.   
  9. <jsp:include page="userform.html"></jsp:include>  
  10.   
  11. </body>  
  12. </html>  

userform.html

 
  1. <a href="viewusers.jsp">View All Records</a><br/>  
  2.   
  3. <h1>Add New User</h1>  
  4. <form action="adduser.jsp" method="post">  
  5. <table>  
  6. <tr><td>Name:</td><td><input type="text" name="name"/></td></tr>  
  7. <tr><td>Password:</td><td>  
  8. <input type="password" name="password"/></td></tr>  
  9. <tr><td>Email:</td><td><input type="email" name="email"/></td></tr>  
  10. <tr><td>Sex:</td><td>  
  11. <input type="radio" name="sex" value="male"/>Male   
  12. <input type="radio" name="sex" value="female"/>Female </td></tr>  
  13. <tr><td>Country:</td><td>  
  14. <select name="country" style="width:155px">  
  15. <option>India</option>  
  16. <option>Pakistan</option>  
  17. <option>Afghanistan</option>  
  18. <option>Berma</option>  
  19. <option>Other</option>  
  20. </select>  
  21. </td></tr>  
  22. <tr><td colspan="2"><input type="submit" value="Add User"/></td></tr>  
  23. </table>  
  24. </form>  

adduser.jsp

 
  1. <%@page import="com.javatpoint.dao.UserDao"%>  
  2. <jsp:useBean id="u" class="com.javatpoint.bean.User"></jsp:useBean>  
  3. <jsp:setProperty property="*" name="u"/>  
  4.   
  5. <%  
  6. int i=UserDao.save(u);  
  7. if(i>0){  
  8. response.sendRedirect("adduser-success.jsp");  
  9. }else{  
  10. response.sendRedirect("adduser-error.jsp");  
  11. }  
  12. %>  

User.java

 
  1. package com.javatpoint.bean;  
  2. public class User {  
  3. private int id;  
  4. private String name,password,email,sex,country;  
  5. //generate getters and setters  
  6. }  

UserDao.java

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 
  1. package com.javatpoint.dao;  
  2. import java.sql.*;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import com.javatpoint.bean.User;  
  6. public class UserDao {  
  7.   
  8. public static Connection getConnection(){  
  9.     Connection con=null;  
  10.     try{  
  11.         Class.forName("com.mysql.jdbc.Driver");  
  12.         con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");  
  13.     }catch(Exception e){System.out.println(e);}  
  14.     return con;  
  15. }  
  16. public static int save(User u){  
  17.     int status=0;  
  18.     try{  
  19.         Connection con=getConnection();  
  20.         PreparedStatement ps=con.prepareStatement(  
  21. "insert into register(name,password,email,sex,country) values(?,?,?,?,?)");  
  22.         ps.setString(1,u.getName());  
  23.         ps.setString(2,u.getPassword());  
  24.         ps.setString(3,u.getEmail());  
  25.         ps.setString(4,u.getSex());  
  26.         ps.setString(5,u.getCountry());  
  27.         status=ps.executeUpdate();  
  28.     }catch(Exception e){System.out.println(e);}  
  29.     return status;  
  30. }  
  31. public static int update(User u){  
  32.     int status=0;  
  33.     try{  
  34.         Connection con=getConnection();  
  35.         PreparedStatement ps=con.prepareStatement(  
  36. "update register set name=?,password=?,email=?,sex=?,country=? where id=?");  
  37.         ps.setString(1,u.getName());  
  38.         ps.setString(2,u.getPassword());  
  39.         ps.setString(3,u.getEmail());  
  40.         ps.setString(4,u.getSex());  
  41.         ps.setString(5,u.getCountry());  
  42.         ps.setInt(6,u.getId());  
  43.         status=ps.executeUpdate();  
  44.     }catch(Exception e){System.out.println(e);}  
  45.     return status;  
  46. }  
  47. public static int delete(User u){  
  48.     int status=0;  
  49.     try{  
  50.         Connection con=getConnection();  
  51.         PreparedStatement ps=con.prepareStatement("delete from register where id=?");  
  52.         ps.setInt(1,u.getId());  
  53.         status=ps.executeUpdate();  
  54.     }catch(Exception e){System.out.println(e);}  
  55.   
  56.     return status;  
  57. }  
  58. public static List<User> getAllRecords(){  
  59.     List<User> list=new ArrayList<User>();  
  60.       
  61.     try{  
  62.         Connection con=getConnection();  
  63.         PreparedStatement ps=con.prepareStatement("select * from register");  
  64.         ResultSet rs=ps.executeQuery();  
  65.         while(rs.next()){  
  66.             User u=new User();  
  67.             u.setId(rs.getInt("id"));  
  68.             u.setName(rs.getString("name"));  
  69.             u.setPassword(rs.getString("password"));  
  70.             u.setEmail(rs.getString("email"));  
  71.             u.setSex(rs.getString("sex"));  
  72.             u.setCountry(rs.getString("country"));  
  73.             list.add(u);  
  74.         }  
  75.     }catch(Exception e){System.out.println(e);}  
  76.     return list;  
  77. }  
  78. public static User getRecordById(int id){  
  79.     User u=null;  
  80.     try{  
  81.         Connection con=getConnection();  
  82.         PreparedStatement ps=con.prepareStatement("select * from register where id=?");  
  83.         ps.setInt(1,id);  
  84.         ResultSet rs=ps.executeQuery();  
  85.         while(rs.next()){  
  86.             u=new User();  
  87.             u.setId(rs.getInt("id"));  
  88.             u.setName(rs.getString("name"));  
  89.             u.setPassword(rs.getString("password"));  
  90.             u.setEmail(rs.getString("email"));  
  91.             u.setSex(rs.getString("sex"));  
  92.             u.setCountry(rs.getString("country"));  
  93.         }  
  94.     }catch(Exception e){System.out.println(e);}  
  95.     return u;  
  96. }  
  97. }  

adduser-success.jsp

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Add User Success</title>  
  6. </head>  
  7. <body>  
  8.   
  9. <p>Record successfully saved!</p>  
  10. <jsp:include page="userform.html"></jsp:include>  
  11.   
  12. </body>  
  13. </html>  

adduser-error.jsp

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Add User Error</title>  
  6. </head>  
  7. <body>  
  8.   
  9. <p>Sorry, an error occurred!</p>  
  10. <jsp:include page="userform.html"></jsp:include>  
  11.   
  12. </body>  
  13. </html>  

viewusers.jsp

 
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  6. <title>View Users</title>  
  7. </head>  
  8. <body>  
  9.   
  10. <%@page import="com.javatpoint.dao.UserDao,com.javatpoint.bean.*,java.util.*"%>  
  11. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  12.   
  13. <h1>Users List</h1>  
  14.   
  15. <%  
  16. List<User> list=UserDao.getAllRecords();  
  17. request.setAttribute("list",list);  
  18. %>  
  19.   
  20. <table border="1" width="90%">  
  21. <tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th>  
  22. <th>Sex</th><th>Country</th><th>Edit</th><th>Delete</th></tr>  
  23. <c:forEach items="${list}" var="u">  
  24. <tr><td>${u.getId()}</td><td>${u.getName()}</td><td>${u.getPassword()}</td>  
  25. <td>${u.getEmail()}</td><td>${u.getSex()}</td><td>${u.getCountry()}</td>  
  26. <td><a href="editform.jsp?id=${u.getId()}">Edit</a></td>  
  27. <td><a href="deleteuser.jsp?id=${u.getId()}">Delete</a></td></tr>  
  28. </c:forEach>  
  29. </table>  
  30. <br/><a href="adduserform.jsp">Add New User</a>  
  31.   
  32. </body>  
  33. </html>  

editform.jsp

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Edit Form</title>  
  6. </head>  
  7. <body>  
  8. <%@page import="com.javatpoint.dao.UserDao,com.javatpoint.bean.User"%>  
  9.   
  10. <%  
  11. String id=request.getParameter("id");  
  12. User u=UserDao.getRecordById(Integer.parseInt(id));  
  13. %>  
  14.   
  15. <h1>Edit Form</h1>  
  16. <form action="edituser.jsp" method="post">  
  17. <input type="hidden" name="id" value="<%=u.getId() %>"/>  
  18. <table>  
  19. <tr><td>Name:</td><td>  
  20. <input type="text" name="name" value="<%= u.getName()%>"/></td></tr>  
  21. <tr><td>Password:</td><td>  
  22. <input type="password" name="password" value="<%= u.getPassword()%>"/></td></tr>  
  23. <tr><td>Email:</td><td>  
  24. <input type="email" name="email" value="<%= u.getEmail()%>"/></td></tr>  
  25. <tr><td>Sex:</td><td>  
  26. <input type="radio" name="sex" value="male"/>Male   
  27. <input type="radio" name="sex" value="female"/>Female </td></tr>  
  28. <tr><td>Country:</td><td>  
  29. <select name="country">  
  30. <option>India</option>  
  31. <option>Pakistan</option>  
  32. <option>Afghanistan</option>  
  33. <option>Berma</option>  
  34. <option>Other</option>  
  35. </select>  
  36. </td></tr>  
  37. <tr><td colspan="2"><input type="submit" value="Edit User"/></td></tr>  
  38. </table>  
  39. </form>  
  40.   
  41. </body>  
  42. </html>  

edituser.jsp

 
  1. <%@page import="com.javatpoint.dao.UserDao"%>  
  2. <jsp:useBean id="u" class="com.javatpoint.bean.User"></jsp:useBean>  
  3. <jsp:setProperty property="*" name="u"/>  
  4. <%  
  5. int i=UserDao.update(u);  
  6. response.sendRedirect("viewusers.jsp");  
  7. %>  

deleteuser.jsp

 
  1. <%@page import="com.javatpoint.dao.UserDao"%>  
  2. <jsp:useBean id="u" class="com.javatpoint.bean.User"></jsp:useBean>  
  3. <jsp:setProperty property="*" name="u"/>  
  4. <%  
  5. UserDao.delete(u);  
  6. response.sendRedirect("viewusers.jsp");  
  7. %>  

Download Project

download CRUD project in JSP


Output

JSP CRUD Example 1 JSP CRUD Example 2 JSP CRUD Example 3 JSP CRUD Example 4 JSP CRUD Example 5 JSP CRUD Example 6 JSP CRUD Example 7

 

[출처] https://www.javatpoint.com/crud-in-jsp

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
126 Creating a CRUD REST API/Service with Spring Boot, JPA and Hibernate file 졸리운_곰 2018.08.17 147
125 Spring boot에서 REST API 개발 시작해보기 file 졸리운_곰 2018.08.17 350
124 [JSP] 네이버 스마트 에디터 연동 + 이미지파일 업로드 기능 추가 file 졸리운_곰 2018.07.28 1123
123 Use React and Spring Boot to Build a Simple CRUD App file 졸리운_곰 2018.07.28 385
» JSP CRUD Example jsp 기본예제 file 졸리운_곰 2018.07.28 166
121 스웨거 2.0으로 스프링 부트 어플리케이션 API 문서화하기 file 졸리운_곰 2018.05.24 132
120 Spring REST API 문서를 Swagger로 만들자 file 졸리운_곰 2018.05.22 411
119 Spring REST API에 Swagger 2 설정하기 file 졸리운_곰 2018.05.22 335
118 Spring Boot와 AngualrJS를 조합한 코드 자동 생성 도구(scaffolding) file 졸리운_곰 2018.05.17 141
117 Spring java Code 기반 설정 Configuration 어노테이션과 Bean 어노테이션 졸리운_곰 2018.05.14 77
116 java spring redis 세션 공유 : Spring Session을 이용한 세션 클러스터링 졸리운_곰 2018.05.06 1041
115 Spring MVC hello world example file 졸리운_곰 2018.05.04 223
114 Create a Web Application With Spring Boot file 졸리운_곰 2018.05.03 142
113 Spring Boot and JSP Tutorial file 졸리운_곰 2018.05.03 427
112 Spring Boot Hello World Example – JSP file 졸리운_곰 2018.05.03 109
111 Spring Boot – JSP View Example file 졸리운_곰 2018.05.03 137
110 SpringMVC-JSP 프로젝트를 Spring Boot로 옮기기 file 졸리운_곰 2018.05.03 319
109 SPRING BOOT에서 JSP 사용하기 졸리운_곰 2018.05.03 65
108 MYBATIS 기본 - SELECTLIST file 졸리운_곰 2018.03.26 79
107 Spring MVC 에 MyBatis 적용해 보기. file 졸리운_곰 2018.03.26 94
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED