CRUD Java Web netbeans mysql

CRUD is an acronym for CREATE, READ, UPDATE and DELETE which are basic functions of persistent storage. CRUD operations can use forms or an interface view to retrieve and return data from a database.

ProceduresFunctionCREATEThis is a form view to create a new record in the databaseREADReads the table records based on the primary key within the input parameter.UPDATEUpdates the content of the table based on the specified primary key for a record.DELETEDeletes a specified row in the table.

Create Application with CRUD Implementation

In this article we will create applications for personal records management with the following technologies:

  • Java Servlets and Java Server Pages (JSP)
  • JSP Standard Tag Library (JSTL)
  • Java Database Connectivity (JDBC)
  • MySQL database
  • Apache Tomcat Server

The following tools can be used for the development:

  1. Eclipse IDE for Java EE Developers (one of the newer versions is recommended)
  2. Apache Tomcat ver 8.5
  3. MySQL Community Server and MySQL Workbench (GUI Tool)
  4. MySQL Connector for Java

Let’s get started with the application, following the instructions below step by step:

1. Create Database

Execute the following MySQL script to create a database named “psmsdb” and a table named ”stuff” (of course, you can named your own database and table):

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

2. Create Project with Maven

In Eclipse IDE, click File > New > Other and Maven Project. Name the project “PSMS”

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

In the Maven POM dialog, fill Group ID , Artifact ID and Description as shown below:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Then add the following dependencies to the pom.xml file:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

3. Writing Model Class

Next, create a model package. We will use the package name com.mitrais.psms.model. Then we can create a Java class named Stuff.java to model a stuff entity in the database with the following code:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

4. Writing DAO Class

  • Create Datasource Class
    CRUD Java Web netbeans mysql
    CRUD Java Web netbeans mysql
  • Create a datasource properties file in folder resources:
    CRUD Java Web netbeans mysql
    CRUD Java Web netbeans mysql
  • Create DAO Interface
    CRUD Java Web netbeans mysql
    CRUD Java Web netbeans mysql
  • Create new interface class to implement DAO Interface
    CRUD Java Web netbeans mysql
    CRUD Java Web netbeans mysql

We now need to implement a DAO (Data Access Object) class that provides CRUD (Create, Read, Update, Delete) operations for the table stuff in the database. Here’s the source code of the DAOStuff class:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

5. Create View/Form file

Create folders for css (in which to save all css/style files) and jsp (in which to save all jsp files) in WebContent folder :

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

CSS File

Create the file style.css in folder css

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

JSP File

Create a page with JSP for displaying all information from the database. The following is code of the JSP file under the WebContentdirectory in the project:

Create a head.jsp file in folder jsp

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Create a file menu.jsp in folder jsp

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Create a file StuffForm.jsp in folder jsp

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Create a file StuffList.jsp in folder jsp

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

6. Creating Controller Servlet Class

Now, we can implement a Java Servlet that acts as a page controller to handle all requests from the client. Let’s look at the code first:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

7. Deploying and Testing the Application

After we have completed the project code we need to deploy and test the application to check that it works.

Right click on project -> Run As -> Run On Server

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

In the pop-up dialog choose Tomcat and then click finish

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

You can type the following url in the web browser to access the Personal Stuff Management Systems Application:

http://localhost:8080/PSMS

For the first time, the list is empty because there isn’t any information contained there yet:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Click on the hyperlink Add New Stuff to begin adding a new stuff:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

Enter stuff information (Name, Description, Quantity and Location) and click Save. The page will redirect to the list pages, as shown below:

CRUD Java Web netbeans mysql
CRUD Java Web netbeans mysql

In this page you will see all items in database, and you can click on the link Edit and Delete to update and remove items.