【JAVAEE】运用MVC+DAO设计查询所有用户信息
本文最后更新于 535 天前,其中的信息可能已经有所发展或是发生改变。

大概的流程如下图:

1.创建DbConnect javaBean类连接数据库。

com/bean/DbConnect.java

[sourcecode language=”java” title=”DbConnect.java”]
package com.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbConnect {
public static Connection getDBconnection() {
String shujuku = "benzhu";
String url1 ="jdbc:mysql://localhost:3306/"+shujuku;
String uname = "root";
String upwd="123456";
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection(url1, uname, upwd);
System.out.println("哈哈哈哈 连接成功");
}
catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void Close(Connection conn,Statement stmt,ResultSet rs) {
try{if(rs!=null) rs.close();}catch(Exception e){e.printStackTrace();}
try{if(stmt!=null) stmt.close();}catch(Exception e){e.printStackTrace();}
try{if(conn!=null) conn.close();}catch(Exception e){e.printStackTrace();}
}
}[/sourcecode]

2.创建User用户javaBean类。
com/bean/User.java

[sourcecode language=”java” title=”User.java”]
package com.bean;

public class User {
private int id;
private String name;
private String pwd;
private int power;
public User() {}
public User(int id,String name,String pwd,int power) {
this.id=id;
this.name=name;
this.pwd=pwd;
this.power=power;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
}[/sourcecode]

3.写UserDao Dao技术运用。
com/dao/UserDao.java

[sourcecode language=”java” title=”UserDao.java”]
package com.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.bean.DbConnect;
import com.bean.User;

public class UserDao {
public ArrayList<User> getAllUsers(){
ArrayList<User> user_list = new ArrayList<User>();
try{
Connection conn;
conn = DbConnect.getDBconnection();
Statement stmt = conn.createStatement();
String sql="select * from benzhu_user";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String pwd = rs.getString(3);
int power = rs.getInt(4);
User user = new User(id,name,pwd,power);
user_list.add(user);
}
DbConnect.Close(conn, stmt, rs);

}
catch(Exception e){
e.printStackTrace();
}
return user_list;

}
}[/sourcecode]

4.写FindServlet servlet类处理浏览器提交的信息。
com/servlet/FindServlet.java

[sourcecode language=”java” title=”FindServlet.java”]
package com.servlet;

import java.io.IOException;
import java.util.ArrayList;

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 com.bean.User;
import com.dao.UserDao;

/**
* Servlet implementation class FindServlet
*/
@WebServlet("/FindServlet")
public class FindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao userdao = new UserDao();
ArrayList<User> user_list = userdao.getAllUsers();
request.setAttribute("user_list",user_list);
request.getRequestDispatcher("user.jsp").forward(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}[/sourcecode]

5.写user JSP显示页面。
user.jsp

[sourcecode language=”html” title=”user.jsp”]
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户查询</title>
</head>
<body>
<form action="FindServlet" method="post">
<input type="submit" value="显示所有注册用户"></form>
<c:if test="${requestScope.user_list!=null }" >
当前所有注册用户为:
<table>
<tr>
<td>id</td>
<td>用户名</td>
<td>用户密码</td>
<td>权限</td>
</tr>
<c:forEach items="${requestScope.user_list }" var="u">

<tr>
<td>${u.id }</td>
<td>${u.name }</td>
<td>${u.pwd }</td>
<td>${u.power }</td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>[/sourcecode]

6.因为jsp运用到了C标签,所以要导入C标签JAR包,下方提供下载。

C标签jar包下载:[bdbtn]https://ge.benzhu.xyz/?/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/html/jakarta-taglibs-standard-1.1.2.zip[/bdbtn]

效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇