SERVLET+JSP+JDBC implementation of querying user information from a database to a page

Source: Internet
Author: User

Project creation here is not the case, just start by displaying the user information list.

Note: I'm using the Servlet3 annotation feature, so I don't need to configure Web. xml

This is my project directory:

First we create the entity class:

public class User implements serializable{
Private static final long serialversionuid = 1L;

Public User () {

}
Private Integer ID;
private String name;
Private String age;
Private String Addrs;

...... Omitted get and Set methods ...

}

Then write a link in the index.jsp page as follows: <a href= "listuser.do" > Show user List </a>.

Then create the Com.servlet.servlet package under SRC and create the Userservlet class,

The @WebServlet ("/listuser.do")//is a servlet annotation used here, listuser.do to be consistent with the links in index.jsp.
public class Userservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Get a list of users
UserService userservice=new UserService ();//Create UserService object,
List List=userservice.getlistall ();
Request.setattribute ("list", list);
Request.getrequestdispatcher ("/jsp/success.jsp"). Forward (request, response);//Jump to success.jsp page

}

}

Then create the corresponding UserService class,

public class UserService {
/**
* Enquiry
* @return
*/
Public list<user> Getlistall () {
Userdao Userdao = new Userdaoimpl ();
List List = Userdao.getlistall ();
return list;
}

}

Finally create the Userdao interface and the Userdaoimpl implementation class

Public interface Userdao {
/**
* Get all Users
* @return List
*/
List<user> Getlistall ();

}

public class Userdaoimpl implements userdao{
/**
* Get the users you use
*/
@Override
Public list<user> Getlistall () {
List<user> list=new arraylist<user> (); //Create a list collection to hold the user object
try {
Connection conn=dbutil.getconnection ();// Call the Getconnection method directly from the tool class to get the JDBC connection.
String sql= "SELECT * from user";
PreparedStatement ps=conn.preparestatement (SQL);
ResultSet Rs=ps.executequery ();
while (Rs.next ()) {
User User=new user (); //Create a user object to keep the data looked up from the data
User.setid (Rs.getint (1));
User.setname (rs.getstring (2));
User.setage (Rs.getstring (3));
User.setaddrs (Rs.getstring (4));
List.add (user);
}
} catch (SQLException e) {

E.printstacktrace ();
}
return list;
}

}

Dbutils tool class for connecting to a database

public class Dbutil {
private static final String url= "Jdbc:mysql://localhost:3306/servlet"; //Database name is servlet
private static final String user= "root";
private static final String password= "1234";
private static Connection Conn=null;
static{
try {
Load Driver
Class.forName ("Com.mysql.jdbc.Driver");
Create a connection
Conn=drivermanager.getconnection (URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {

E.printstacktrace ();
} catch (SQLException e) {

E.printstacktrace ();
}
}
public static Connection getconnection () {

Return conn;
}

}

Finally we create a success.jsp page that returns user information

<body>
<table border=1 cellpadding= "cellspacing=" 0 ">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>addrs</th>
</tr>

<c:foreach items= "${list}" var= "user" >//use jstl tag and El expression to display user information
<tr>
<th>${user.id}</th>
<th>${user.name}</th>
<th>${user.age}</th>
<th>${user.addrs}</th>
</tr>
</c:forEach>

</table>
</body>

Finally we create the corresponding database and the user data table, then add a few data, run the next project.

SERVLET+JSP+JDBC implementation of querying user information from a database to a page

Related Article

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.