Java Struts2 and Hibernate4 CRUD With MySQL With Pagination, Sorting and Export Option Using Netbeans
Java Struts2 and Hibernate4 CRUD With MySQL With Pagination, Sorting and Export Option Using Netbeans
Struts2HibernateCRUD.rar
Introduction
This simple Application helps to Create, Read, Update and Delete (CRUD) Application, operating
on the ‘contacts’ table in the ‘test’ database in MySQL Database Server. It is a hibernate-
annotation based Application. There is an option to delete more than one record in a Webpage at
once.
1. JDK8u25
2. Netbeans 8.02
3. MySQL 5.*(or XAMPP)
4. MySQL Connector 5.*
5. Hibernate 4.3.** (Bundled with Netbeans)
6. Display Tag Library(For pagination, sorting and export)
Steps are-
After installing Netbeans, click the Services tab on the left. Expand the Database node. Expand
the Drivers node. Right-click MySQL(Connector/Jdriver) and then connect. Put the test as the
database, as shown below. Put the password, if you have given the password at the time of
installation of MySQL database Server. For XAMPP, no password was given, followed by the test
connection. If successful, click Finish button.
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 1/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Create ‘contacts’ table, using the script, given below, in MySQL ‘test’ Database-
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 2/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Project Structure
Download and add the following libraries(JAR) one by one by right-clicking the libraries folder in
project Window, followed by Add JAR/Folder. Most of the files are provided for the download.
1. MySQL-Connector.java-5.1.35-bin.jar
2. Struts2-jquery-plugin-2.3.1.jar
3. displaytag-1.2.jar
4. displaytag-export-poi-1.2.jar
5. displaytag-portlet-1.2.jar
6. commons-beanutilis-1.8.0.jar
7. commons-collections-3.1.jar
8. commons-lang3-3.4.jar
9. commons-digester-2.0.jar
10. commons-lang-2.4.jar
11. commons-logging-1.1.3.jar
12. commons-logging-api-1.1.jar
13. xwork-core-2.3.24.1.jar
14. struts2-core-2.3.24.1.ja
15. struts2-convention-plugin-2.3.24.1.jar
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 3/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
16. ognl-3.0.6.jar
17. freemaker-2.3.22.jar
18. commons-lang3-3.2.jar
19. commons-io-2.2.jar
20. commons-fileupload-1.3.1.jar
21. asm-tree-3.3.jar
22. asm-commons-3.3.jar
23. asm-3.3.jar
1. hibernate.cfg.xml
Here, XAMPP is used without the password, so there is no password. Otherwise, the
password for connecting to the database is required.
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 4/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
configuration-3.0.dtd">
03. <hibernate-configuration>
04. <session-factory>
05. <property name="hibernate.dialect">org.hibernate.dialect.MySQLD
06. <property name="hibernate.connection.driver_class">com.mysql.jd
07. <property name="hibernate.connection.url">jdbc:mysql://localhos
zeroDateTimeBehavior=convertToNull</property>
08. <property name="hibernate.connection.username">root</property>
09. <property name="connection.password"></property>
10. <property name="connection.pool_size">10</property>
11. <!-- Enable Hibernate's automatic session context management -
->
12. <property name="current_session_context_class">thread</property
13. <property name="cache.provider_class">org.hibernate.cache.NoCac
14. <!-- Display all generated SQL to stdout -->
15. <property name="show_sql">true</property>
16. <property name="hbm2ddl.auto">update</property>
17. <!-- Mapping with model class containing annotations -->
18. <mapping class="com.pojos.model.Contacts"/>
19. </session-factory>
20. </hibernate-configuration>
Copy and paste the code of the file given below, whose code is not generated.
CODE
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 5/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Important: To create this file, MySQL database tests most to be connected through
Netbeans. Right-click com.model package--new-Hibernate Mapping Files and POJOs from
the database-click Finish.
Connect to the database. The Window, given below, opens. Check EJB3.0 annotation box.
Uncheck Hibernate XML Mapping checkbox. Choose package com.pojos.model.
12.
13. /**
14. * Contacts generated by hbm2java
15. */
16. @Entity
17. @Table(name="contacts"
18. ,catalog="test"
19. )
20. public class Contacts implements java.io.Serializable {
21. private Integer id;
22. private String firstname;
23. private String lastname;
24. private String sex;
25. private String cellno;
26. private String emailId;
27. private String country;
28. private String website;
29. private Date birthdate;
30. private Date created;
31.
32. public Contacts() {
33. }
34.
35. public Contacts(Date created) {
36. this.created = created;
37. }
38. public Contacts(String firstname, String lastname, String sex,
39. this.firstname = firstname;
40. this.lastname = lastname;
41. this.sex = sex;
42. this.cellno = cellno;
43. this.emailId = emailId;
44. this.country = country;
45. this.website = website;
46. this.birthdate = birthdate;
47. this.created = created;
48. }
49.
50. @Id @GeneratedValue(strategy=IDENTITY)
51. @Column(name="id", unique=true, nullable=false)
52. public Integer getId() {
53. return this.id;
54. }
55.
56. public void setId(Integer id) {
57. this.id = id;
58. }
59.
60.
61. @Column(name="firstname", length=30)
62. public String getFirstname() {
63. return this.firstname;
64. }
65. public void setFirstname(String firstname) {
66. this.firstname = firstname;
67. }
68. @Column(name="lastname", length=30)
69. public String getLastname() {
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 7/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Right-click on com. dao package-new-JavaClass. Give the class name as ContactDao and
click Finish.
37. try {
38. Contacts contact = (Contacts) session.load(Contacts.class,i
39. if(null != contact) {
40. session.delete(contact);
41. }
42. } catch (HibernateException e) {
43. e.printStackTrace();
44. session.getTransaction().rollback();
45. }
46. session.getTransaction().commit();
47.
48. session.flush();
49. session.close();
50. }
51.
52. public void update(Contacts contact) {
53. Session session = HibernateUtil.getSessionFactory().openSes
54. session.beginTransaction();
55. //Contacts contact = (Contacts) session.load(Contacts.class,
56. try {
57. if(contact != null) {
58. session.saveOrUpdate(contact);
59. }
60.
61. } catch (HibernateException e) {
62. e.printStackTrace();
63. session.getTransaction().rollback();
64. }
65. session.getTransaction().commit();
66. session.flush();
67. session.close();
68. }
69.
70. public int getNewContactId() {
71. Session session = HibernateUtil.getSessionFactory().ge
72. Transaction trans = session.beginTransaction();
73. String query = "SELECT max(c.id) FROM Contacts c";
74. List list = session.createQuery(query).list();
75. int maxId = ((Integer) list.get(0));
76.
77. trans.commit();
78. session.close();
79. return (maxId+1);
80. }
81.
82.
83. public List<Contacts> list(){
84. Session session = HibernateUtil.getSessionFactory().openSessio
85. List<Contacts> DaoAllContacts = null;
86. session.beginTransaction();
87. try {
88.
89. DaoAllContacts = session.createCriteria(Contacts.cla
90. //DaoAllContacts = (List<Contacts>)session.createQue
91. int count =DaoAllContacts.size();
92. System.out.println("No of Record From Dao: " + count)
93. } catch (HibernateException e) {
94. e.printStackTrace();
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 10/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
95. session.getTransaction().rollback();
96. }
97. session.getTransaction().commit();
98. session.flush();
99. session.close();
100. return DaoAllContacts;
101.
102. }
103.
104. }
5. HibernateUtil.java File
6. web.xml (Automatically generated, followed by copying and pasting the code, given below)-
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 11/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
31.
32. @Override
33. public String execute() {
34. this.ContactList = dao.list();
35.
36. int count = ContactList.size();
37. System.out.println("contactList Size"+ count);
38. //System.out.println("execute called");
39. return SUCCESS;
40. }
41. public String add() throws ParseException {
42. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-
dd",Locale.ENGLISH);
43. Date dob = sdf.parse(getBirthdate());
44. contact.setCreated(date);
45. contact.setBirthdate(dob);
46. System.out.println(birthdate);
47.
48. System.out.println(contact);
49. try {
50.
51. dao.add(contact);
52.
53. } catch (Exception e) {
54. e.printStackTrace();
55. }
56. this.ContactList = dao.list();
57. return SUCCESS;
58. }
59.
60. public String update() throws ParseException{
61. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-
dd");
62. Date dob = sdf.parse(getBirthdate());
63. contact.setBirthdate(dob);
64. contact.setCreated(date);
65. System.out.println(getContact());
66. try {
67.
68. dao.update(contact);
69. } catch (Exception e) {
70. e.printStackTrace();
71. }
72. this.ContactList = dao.list();
73. return SUCCESS;
74. }
75.
76. public String removeContact() throws ParseException {
77.
78. try {
79.
80. System.out.println("No of Selected Record:-
" + Checkbox.length);
81. for (int i=0;i<Checkbox.length; i++){
82. System.out.println("Selected RecordId:-
" + Checkbox[i]);
83. dao.deleteContact((Checkbox[i]));
84.
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 13/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
85. }
86.
87.
88. } catch (Exception e) {
89. e.printStackTrace();
90. }
91. this.ContactList = dao.list();
92. return SUCCESS;
93. }
94.
95.
96. public String deleteContact() {
97.
98. System.out.println("id value="+contact.getId());
99. int id = contact.getId();
100. try {
101.
102. dao.deleteContact(id);
103. } catch (Exception e) {
104. e.printStackTrace();
105. }
106. this.ContactList = dao.list();
107. return SUCCESS;
108. }
109.
110. public Contacts getContact() {
111. return contact;
112. }
113.
114. public void setContact(Contacts contact) {
115. this.contact = contact;
116. }
117.
118. public List<Contacts> getContactList() {
119. return ContactList;
120. }
121. public void setContactList(List<Contacts> ContactList) {
122. this.ContactList = ContactList;
123. }
124.
125. public int getId() {
126. return id;
127. }
128.
129. public void setId(int id) {
130. this.id = id;
131. }
132.
133. public String getBirthdate() {
134. return birthdate;
135. }
136.
137. public void setBirthdate(String birthdate) {
138. this.birthdate = birthdate;
139. }
140. public Integer[] getCheckbox() {
141. return Checkbox;
142. }
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 14/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
8. struts.xml File
index.jsp code
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 16/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
75. </display:column>
76.
77.
78. <s:url id="delContact" action="deleteContact">
79. <s:param name="contact.id" value="%{#attr.row.id}" />
80. </s:url>
81. <display:column title="Action">
82. <s:a href="%
{delContact}" onclick="return confirm('Are you sure you want to del
83. </display:column>
84. </display:table>
85. </s:form>
CRUD.jsp
Update.jsp
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 19/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Delete.jsp
Click Delete link to delete the corresponding record. Select the record for deletion by selecting
corresponding checkboxes and hit DeleteSelected button. Few columns are hidden and can be
seen by clicking Display&Update Link.
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 20/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Sorting
Download Now!
SIMILAR ARTICLES
JSP and Hibernate CRUD application With Pagination, Sorting and export option using NetBeans IDE And
MySQL 5
Primefaces5, Spring4, Hibernate4, CRUD Using Netbeans 8.02 And MySQL5 Database Server
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 21/22
11/30/23, 9:46 AM Java Struts2 And Hibernate4 CRUD With MySQL With Pagination, Sorting And Export Option Using Netbeans
Raichand Ray
https://www.c-sharpcorner.com/members/raichand-ray
970 590.4k
Type your comment here and press Enter Key (Minimum 10 characters)
olahraga
Ikutio lahragannasional
asional
yangtidak
tidakresmi
resmidi
di
Singapura,yaituyaitu
Kulineran.
CarouselBuffetRestaurant
About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners
Web3 Universe Build with JavaScript Let's React DB Talks Jumpstart Blockchain interviews.help
©2023 C# Corner. All contents are copyright of their authors.
https://www.c-sharpcorner.com/article/java-struts2-and-hibernate4-curd-with-mysql-with-pagination-sorting-and-export/ 22/22