1. View
a. JSP
b. JavaScript
2. Model
a. Java DTO
b. Java Classes to interact with database
3. Controller
a. Servlet
----------------------------------------------------------------------------
This is a simple Add/Delete/Update/View records into a table in DB2.
Step 1 : Create the home.jsp. This JSP is the main JSP, which will have the records and options for delete and update and add links. The Screen will look like this.
Add New Music | ||||||||
Music Name | Year | Edit | Delete | |||||
No Record Avaliable |
Step 2: Create JSP for Adding new record. ( When user clicks on the Add New Music link in above screen)
The Screen will look like this.
Step 3: Create servlet to control the flow from JSP to the back end.
package org.test.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/add")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path ="/music";
RequestDispatcher dispater = getServletContext().getRequestDispatcher(path);
dispater.forward(request, response);
//response.sendRedirect(path);
}
}
Step3 : Create main servlet.
package org.test.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.test.dto.Album;
/**
* Servlet implementation class MusicServlet
*/
@WebServlet("/music")
public class MusicServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path ="/Home.jsp";
RequestDispatcher dispater = getServletContext().getRequestDispatcher(path);
ArrayList
Album album = new Album();
album.setName("XYZ");
album.setYear("1977");
list.add(album);
Album album1 = new Album();
album1.setName("ABCD");
album1.setYear("1977");
list.add(album1);
request.setAttribute("list", list);
dispater.forward(request, response);
}
}
This will complete the Adding a new record into the table. The code for DB2 connectivity is written below. This needs to be integrated in the above code.
/**
* ConnectDB2.java
*/
package com.javaworkspace.connectdb2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @author www.javaworkspace.com
*
*/
public class ConnectDB2 {
public static void main(String[] args) {
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection(
"jdbc:db2://localhost:5021/EMPLOYEE", "username",
"password");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next()) {
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
-----------------------------------------------
No comments:
Post a Comment