简易用户增,删,改,查(DAO模式实现):
简单介绍:
- Conn.java:连接数据库
- UserDao.java(数据层):放增,删,改,查的方法
- User.java:JavaBean定义,封装用户属性的类,注意命名要与表中变量名一致
- view.jsp:初始界面,进行选择
- Register.jsp:添加注册页面
- DoRegister.jsp:执行添加操作
- SelectUser.jsp:用来显示全部用户及查找,还可用于选择删除和修改操作
- DeleteUser.jsp:删除用户
- UpdateUser.jsp:修改用户
- DoUpdateUser.jsp:执行修改用户操作
话不多说上代码:
- Conn.java
package com.JavaBean_zyd.conn;
import java.sql.*;
public class Conn {
public Connection getConn() {
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testaa?characterEncoding=utf-8", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
package com.JavaBean_zyd.dao;
import com.JavaBean_zyd.conn.*;
import com.JavaBean_zyd.user.*;
import java.sql.*;
import java.util.*;
public class UserDao {
public int InsertUser(User user) {
int rs=0;
try {
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_insert="INSERT INTO user(username,password,type) VALUES (?,?,?) ";
PreparedStatement pst = conn.prepareStatement(sql_insert);
pst.setString(1,user.getUsername());
pst.setString(2, user.getPassword());
pst.setString(3,user.getType());
rs=pst.executeUpdate();
pst.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public List<User> SelectUser() {
List<User> list = new ArrayList<User>();
ResultSet rs = null;
try {
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_select="select * from user ";
PreparedStatement pst = conn.prepareStatement(sql_select);
rs=pst.executeQuery();
while(rs.next()) {
User user = new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setType(rs.getString("type"));
list.add(user);
}
rs.close();
pst.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return list;
}
public int DeleteUser(User user) {
int rs=0;
try {
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_delete="delete from user where id = ?";
PreparedStatement pst = conn.prepareStatement(sql_delete);
pst.setString(1,user.getId());
rs=pst.executeUpdate();
pst.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
public User SelectUserId(User user){
ResultSet rs=null;
User usera=new User();
try{
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_select_id="select * from user where id=?";
PreparedStatement pst=conn.prepareStatement(sql_select_id);
pst.setString(1, user.getId());
rs=pst.executeQuery();
if(rs.next()){
usera.setId(rs.getString("id"));
usera.setUsername(rs.getString("username"));
usera.setPassword(rs.getString("password"));
usera.setType(rs.getString("type"));
}
rs.close();
pst.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
return usera;
}
public int UpdateUser(User user){
int rs=0;
try{
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_insert="update user set username=?,password=? where id=?";
PreparedStatement pst=conn.prepareStatement(sql_insert);
pst.setString(1, user.getUsername());
pst.setString(2, user.getPassword());
pst.setString(3, user.getId());
rs=pst.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
public User LoginUser(User user){
User usera=new User();
ResultSet rs=null;
try{
Conn dbc=new Conn();
Connection conn=dbc.getConn();
String sql_select="select * from user where username=? and password=?";
PreparedStatement pst=conn.prepareStatement(sql_select);
pst.setString(1, user.getUsername());
pst.setString(2, user.getPassword());
rs=pst.executeQuery();
if(rs.next()){
usera.setId(rs.getString("id"));
usera.setUsername(rs.getString("username"));
usera.setPassword(rs.getString("password"));
usera.setType(rs.getString("type"));
}
}
catch(Exception e){
e.printStackTrace();
}
return usera;
}
}
package com.JavaBean_zyd.user;
public class User {
private String id;
public String getId() {
return id;
}
public void setId(String 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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String username;
private String password;
private String type;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>选择界面</title>
</head>
<body>
<center>
<h3>javaBean选择界面(增删改查)</h3>
<hr>
<table border="1">
<tr>
<td><a href="Register.jsp">**添加用户**</a></td>
</tr>
<tr>
<td><a href=" SelectUser1.jsp">**删除用户**</a></td>
</tr>
<tr>
<td><a href="SelectUser1.jsp">**修改用户**</a></td>
</tr>
<tr>
<td><a href="SelectUser1.jsp">**显示用户**</a></td>
</tr>
</table>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面(数据库)</title>
<script language="javascript">
function check(){
var password=form.password.value;
if(password==""){
alert("密码不能为空!!!");
form.password.focus();
return;
}
}
</script>
</head>
<body>
<center>
<h1>添加用户</h1>
<hr>
<%--<form action="DoRegl.jsp" method="post" name="form" onsubmit="return check()"> --%>
<form action="DoRegister.jsp" method="post" name="form" onsubmit="return check()">
<table border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value=""></td></tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value=""></td></tr>
<tr>
<td>类型:</td>
<td><select name="type">
<option value="Admin">Admin</option>
<option value="User">User</option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="登录"/>
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.JavaBean_zyd.dao.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加业务层</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="user" class="com.JavaBean_zyd.user.User"/>
<jsp:setProperty property="*" name="user"/>
<%=user.getUsername() %> 添加成功! <%--测试输出 --%>
<%
UserDao userinsert = new UserDao();
int rs = userinsert.InsertUser(user);
if(rs!=0){
out.println("成功添加"+rs+"条记录!!!");
}
%>
<br>
<a href="view.jsp"><font size="3">返回选择界面</font></a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.JavaBean_zyd.user.*,com.JavaBean_zyd.dao.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查找业务层</title>
</head>
<body>
<%
UserDao userselect = new UserDao();
List<User> listall = new ArrayList<User>();
listall = userselect.SelectUser();
Iterator<User> iter = listall.iterator();
%>
<table border="1">
<tr><td>用户名</td><td>密码</td><td>类型</td></tr>
<%
while(iter.hasNext()){
User user = iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getPassword() %></td>
<td><%=user.getType() %></td>
<td><a href="DeleteUser.jsp?id=<%=user.getId()%>">删除操作</a></td>
<td><a href="UpdateUser.jsp?id=<%=user.getId()%>">修改操作</a></td>
</tr><%
}%>
</table>
<br><a href="view.jsp"><font size="3">返回首页</font></a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.JavaBean_zyd.user.*,com.JavaBean_zyd.dao.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查找业务层</title>
</head>
<body>
<%
UserDao userselect = new UserDao();
List<User> listall = new ArrayList<User>();
listall = userselect.SelectUser();
Iterator<User> iter = listall.iterator();
%>
<table border="1">
<tr><td>用户名</td><td>密码</td><td>类型</td></tr>
<%
while(iter.hasNext()){
User user = iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getPassword() %></td>
<td><%=user.getType() %></td>
<td><a href="DeleteUser.jsp?id=<%=user.getId()%>">删除操作</a></td>
<td><a href="UpdateUser.jsp?id=<%=user.getId()%>">修改操作</a></td>
</tr><%
}%>
</table>
<br><a href="view.jsp"><font size="3">返回首页</font></a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.JavaBean_zyd.user.*,com.JavaBean_zyd.dao.*,java.util.*,java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
UserDao userselectid=new UserDao();
User user=new User();
user.setId(request.getParameter("id"));
User usera=new User();
usera=userselectid.SelectUserId(user);
%>
<form action="DoUpdateUser.jsp?id=<%=usera.getId()%>" method="post">
用户名:<input type="text" name="username" value="<%=usera.getUsername()%>">
密码:<input type="text" name="password" value="<%=usera.getPassword()%>">
用户类型:<input type="text" name="type" value="<%=usera.getType()%>">
<input type="submit" value="修改">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.JavaBean_zyd.user.*,com.JavaBean_zyd.dao.*,java.util.*,java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
%>
<jsp:useBean id="usera" class="com.JavaBean_zyd.user.User"></jsp:useBean>
<jsp:setProperty property="*" name="usera"></jsp:setProperty>
<%
usera.setId(request.getParameter("id"));
UserDao userupdate=new UserDao();
int rs=userupdate.UpdateUser(usera);
if(rs!=0){
response.sendRedirect("SelectUser1.jsp");
}
else{
out.println("更新失败");
}
%>
</body>
</html>
!!!截图!!!