MVC mode (Model View Controller) to implement database connection, the deletion of data, check operation

Source: Internet
Author: User

MVC mode (Model View Controller):

Model:dao model

view:jsp on the page to fill out the Java Code implementation display

Controller:servlet

Redirection and forwarding of requests:

If the corresponding page of the target does not need to read any information from the request, the redirect can be used to prevent the form from repeating the submission;

------------------------------------------------------------------------------------------------

Student.java class. It encapsulates the information of many attributes;

Package com.lanqiao.javatest;

public class Student {
Private Integer ID;
Private String username;
private String password;
Public Student () {
Super ();
}
Public Student (Integer ID, string username, string password) {
Super ();
This.id = ID;
This.username = Username;
This.password = password;
}
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
@Override
Public String toString () {
Return "Student [id=" + ID + ", username=" + Username + ", password=" + password + "]";
}

}

------------------------------------------------------------------------------------------------------

Studentdao.java class. The implementation of the database connection, and the implementation of the data query and delete, you can modify the implementation of additions and deletions of the function

public class Studentdao {

How to connect to a database and implement a delete
public void Deleteid (Integer id) {

Connection Connection=null;
PreparedStatement Preparedstatement=null;
String sql= "Delete from person where id=?";
try {
String driverclass= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql:///test";
String user= "root";
String password= "lxn123";

Class.forName (Driverclass);
Connection=drivermanager.getconnection (URL, user, password);
Preparedstatement=connection.preparestatement (SQL);
Preparedstatement.setint (1, id);
Preparedstatement.executeupdate ();


} catch (Exception e) {

}finally {
if (preparedstatement!=null) {
try {
Preparedstatement.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (connection!=null) {
try {
Connection.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

}


How to connect to a database and implement a query
Public list<student> GetAll () {
List<student> list=new arraylist<student> ();

This method gets the method to connect to the database
Connection Connection=null;
PreparedStatement Preparedstatement=null;
ResultSet Resultset=null;

String sql= "select Id,username,password from";
try {
String driverclass= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql:///test";
String user= "root";
String password= "lxn123";

Class.forName (Driverclass);
Connection=drivermanager.getconnection (URL, user, password);
Preparedstatement=connection.preparestatement (SQL);
Resultset=preparedstatement.executequery ();
while (Resultset.next ()) {
int Id=resultset.getint (1);
String username=resultset.getstring (2);
String password1=resultset.getstring (3);
Student student=new Student (id,username,password1);
List.add (student);
}

} catch (Exception e) {
E.printstacktrace ();
}
finally {
if (resultset!=null) {
try {
Resultset.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (preparedstatement!=null) {
try {
Preparedstatement.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (connection!=null) {
try {
Connection.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
return list;
}
}

-----------------------------------------------------------------------------------------------

Listallstudent.servlet class. Implemented, call the top of the Studentdao class inside the query method, set the property name of the property, and the value of the property: Request.setattribute ("Student", students); Forward the request to the Student.jsp page, and hyperlink to the Test.jsp page, student.jsp the page to insert Java code, to implement the function of the query

public class Listallstudent extends HttpServlet {
Private static final long serialversionuid = 1L;

protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Studentdao studentdao=new Studentdao ();

List<student> Students=studentdao.getall ();


Request.setattribute ("Student", students);

Forwarding of requests
Request.getrequestdispatcher ("/students.jsp"). Forward (request, response);


}

}

------------------------------------------------------------------------------------------------

student.jsp page Implementation database data is displayed on the page:

<% @page import= "Com.lanqiao.javatest.Student"%>
<% @page import= "Java.util.List"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<%
List<student> list= (list<student>) request.getattribute ("Student");
%>
<table border= "1" cellspacing= "0" cellpadding= "ten" >
<tr>
<th>Id</th>
<th>userName</th>
<th>password</th>
<th>Delete</th>
</tr>
<%for (Student student:list) {%>
<tr>
<td><%=student.getid ()%></td>
<td><%=student.getusername ()%></td>
<td><%=student.getpassword ()%></td>
<td><a href= "Deletestudent?id=<%=student.getid ()%>" >delete</a></td>
<tr>
<%}%>
</table>
</body>

------------------------------------------------------------------------------------------------

Deletestudent.servlet class. Get the ID value of the property queried by the student.jsp page hyperlink,//string id=request.getparameter ("id"), call the Delete method inside the Studentdao class, the function that has been implemented, Page display after the request is forwarded to the delete.jsp page for deletion

public class Deletestudent extends HttpServlet {
Private static final long serialversionuid = 1L;

protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Get hyperlink <a href= "Deletestudent?id=<%=student.getid ()%>" >delete</a> inside ID value
String id=request.getparameter ("id");
Studentdao studentdao=new Studentdao ();

Integer.parseint (ID) converts a string type to an integer type
Studentdao.deleteid (Integer.parseint (id));

Forwarding of requests
Request.getrequestdispatcher ("/delete.jsp"). Forward (request, response);

}

}

----------------------------------------------------------------------------------------------

The delete.jsp page implements the page that is displayed after the deletion, and hyperlinks to the page that starts the query, viewing the deleted data

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<br><br>
<a href= "Listallstudent" >list all Student page</a>
</body>

-----------------------------------------------------------------------------------------------

The test.jsp page implements the query function:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<a href= "Listallstudent" >list all Student page</a>
</body>

-------------------------------------------------------------------------------------------------------

To implement a functional process:

The page implements a link:

Click the link to display the database data that you queried:

Click Delete after each table to implement the deletion function:

After clicking on the link, find out the remaining data after deletion

When data in the database is not deleted:

After implementing the function is:

------------------------------------------------------------------------------------------------

In the Web. xml file below Lib, there are two configurations and mappings. Configuration and mapping enables the association between servlet classes and JSPs

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 ">


<servlet>
<description></description>
<display-name>ListAllStudent</display-name>
<servlet-name>ListAllStudent</servlet-name>
<servlet-class>com.lanqiao.javatest.ListAllStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListAllStudent</servlet-name>
<url-pattern>/listAllStudent</url-pattern>
</servlet-mapping>


<servlet>
<description></description>
<display-name>DeleteStudent</display-name>
<servlet-name>DeleteStudent</servlet-name>
<servlet-class>com.lanqiao.javatest.DeleteStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteStudent</servlet-name>
<url-pattern>/deleteStudent</url-pattern>
</servlet-mapping>
</web-app>

MVC mode (Model View Controller) to implement database connection, data deletion, check operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.