The MVC design mode implements the database connection and gets all the data to be displayed on the browser page

Source: Internet
Author: User

Implement a Java class that builds a student: it encapsulates all attributes of the attribute;

public class Student {
private int id;
Private String username;
private String password;
Public Student () {
Super ();
}
public Student (int ID, string username, string password) {
Super ();
This.id = ID;
This.username = Username;
This.password = password;
}
public int getId () {
return ID;
}
public void setId (int 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 + "]";
}

}

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

Create a Java class, implement database connection, and implement query function, query all the values of all properties, and return a list to undertake;

public class Studentdao {
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;
}
}

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

Set up a servlet class to get all the information in the database from the GetAll method inside the class Studentdao, and the function of forwarding the request

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);

The forwarded address of the request
Request.getrequestdispatcher ("/students.jsp"). Forward (request, response);
}

}

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

The address of the request forwarding is here students.jsp: is a JSP file

<% @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> names= (list<student>) request.getattribute ("Student");
%>

<table border= "1" >
<tr>
<th>Id</th>
<th>userName</th>
<th>password</th>
</tr>
<%for (Student list:names) {%>
<tr>
<td><%=list.getid ()%></td>
<td><%=list.getusername ()%></td>
<td><%=list.getpassword ()%></td>
<tr>
<%}%>
</table>

</body>

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

is a JSP file, test.jsp, is a hyperlink, its content is displayed on the browser page, click to achieve this project requirements of the 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>

Before clicking the hyperlink:

After clicking on the hyperlink:

The source files in the database are:

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

Under Lib, the contents of the Web. xml file are: Inside settings and reflections to obtain information;

<?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>
</web-app>

The MVC design mode implements the database connection and gets all the data to be displayed on the browser page

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.