结构:
UserDAO.java:
package com.demo.dao;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.example.ConnectDB;
import com.example.Reader;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class UserDAO {
//数据库连接对象
public static Reader login(String name,String password) throws SQLException {
Reader u=null;
java.sql.Connection connection =null;
PreparedStatement pstmt=null;
ResultSet resultSet=null;
//赋值
try {
connection= DriverManager.getConnection(
"jdbc:MySQL://127.0.0.1:3306/test", "root", "123");
//静态sql语句
String sql = "select * from user where name=? and password=?";
pstmt = (PreparedStatement) connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
resultSet = pstmt.executeQuery();
if(resultSet.next()){
u=new Reader();
u.setName(resultSet.getString("name"));
u.setPassword(resultSet.getString("password"));
System.out.println("登录成功!");
}else{
System.out.println("用户名或者密码错误!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectDB.endConnect();
}
return u;
} }
LoginServlet.java:
package com.demo.service;
import java.io.IOException;
import java.sql.SQLException;
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 javax.swing.JOptionPane;
import com.example.Reader;
import com.demo.dao.UserDAO;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
UserDAO userDAO=new UserDAO();
Reader user = null;
try {
user = UserDAO.login(name, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user!=null){
request.getRequestDispatcher("success.jsp").forward(request, response);;
}else{
//alert("Warning Message");
//JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
}
ConnectDB.java
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.example.Reader;
public class ConnectDB {
private static Connection conn = null; //连线
private static Statement stmt = null; //操作指令 java就是和cmd的中介
private static ResultSet rs = null; //传回来的表
public static void startConnect() {
try {
Class.forName("com.mysql.jdbc.Driver"); //类别要被含进来
try {
conn = DriverManager.getConnection(
"jdbc:MySQL://127.0.0.1:3306/test", "root", "123");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void endConnect() throws SQLException {
//关闭连接
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
public static void update(String sql) throws SQLException {
//数据库更新
startConnect(); //连接mysql
stmt = conn.createStatement(); //创建链接的中介
stmt.executeUpdate(sql); //执行操作指令
endConnect(); //关闭连接
}
public static ArrayList getList1(String sql) throws SQLException {
//数据库查询
ArrayList list = new ArrayList();
startConnect();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Reader rd = new Reader();
rd.setId(rs.getString("id"));
rd.setName(rs.getString("name"));
rd.setPassword(rs.getString("password"));
list.add(rd);
}
endConnect();
return list;
}
}
Reader.java
package com.example;
public class Reader {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
add.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册账号</title>
<style type="text/css">
body{
background:url(pictures/bg_sign.jpg) no-repeat left top;
background-size:100%;
}
</style>
</head>
<body>
<div style="text-align:center;margin-top:120px">
<h1 >请注册</h1>
<form action="addinfo.jsp" method="post">
<table style="margin-left:42%">
<tr>
<td>ID:</td>
<td> <input type="text" name="id"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"> </td>
</tr>
</table>
<input type="submit" name="submit" value="注册">
<input type="reset" value="重置">
</form>
<br>
<a href="list.jsp"><h2>查看</h2></a>
<a href="login.jsp"><h2>返回</h2></a>
</div>
</body>
</html>
addinfo.jsp
<%@page import="com.example.ConnectDB"%>
<%@ page language="java" import="com.example.ConnectDB" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String id=new String(request.getParameter("id").getBytes("UTF-8"));
String name = new String(request.getParameter("name").getBytes("UTF-8"));
String password = new String(request.getParameter("password").getBytes("UTF-8"));
ConnectDB.update("insert into user (id,name,password) values ('"+id+"','"+name+"','"+password+"')");
response.sendRedirect("list.jsp");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'addinfo.jsp' starting page</title>
</head>
<body>
添加成功!<br>
</body>
</html>
change.jsp
<%@ page language="java" import="com.example.ConnectDB" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String oid =new String(request.getParameter("id").getBytes("UTF-8"));
String oname = new String(request.getParameter("name").getBytes("UTF-8"));
String opassword = new String(request.getParameter("password").getBytes("UTF-8"));%>
<%
String updateUser="update user set name= '"+oname+"' where id="+oid;
ConnectDB.update(updateUser);
String updateUser1="update user set password= '"+opassword+"' where id="+oid;
ConnectDB.update(updateUser1);
response.sendRedirect("list.jsp"); //转到list.jsp页面
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'delete.jsp' starting page</title>
</head>
<body>
This is my JSP page.<br>
</body>
</html>
delete.jsp
<%@ page language="java" import="com.example.ConnectDB" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String oid =new String(request.getParameter("id").getBytes("UTF-8"));
String oname = new String(request.getParameter("name").getBytes("UTF-8"));
String opassword = new String(request.getParameter("password").getBytes("UTF-8"));%>
<%
String updateUser="update user set name= '"+oname+"' where id="+oid;
ConnectDB.update(updateUser);
String updateUser1="update user set password= '"+opassword+"' where id="+oid;
ConnectDB.update(updateUser1);
response.sendRedirect("list.jsp"); //转到list.jsp页面
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'delete.jsp' starting page</title>
</head>
<body>
This is my JSP page.<br>
</body>
</html>
error.jsp
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
用户名或者密码错误!
</body>
</html>
list.jsp
<%@page import="com.example.Reader"%>
<%@page import="com.example.ConnectDB"%>
<%@ page language="java" import="com.example.ConnectDB,com.example.Reader,java.util.*"
pageEncoding="UTF-8"%>
<%
ArrayList list = ConnectDB.getList1("select id,name,password from user"); //获得test表中所有数据并以列表形式返回
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>后台查看用户信息</title>
</head>
<body>
<input type="button" name="bottom" value="添加新读者"
onclick="javascript:window.location.href='add.jsp'">
<br>
<br>
<table border="1">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
密码
</td>
<td>
操作
</td>
</tr>
<%
{
for (Iterator it = list.iterator(); it.hasNext();) {
Reader r = (Reader) it.next();
%>
<tr>
<td><%=r.getId()%></td>
<td><%=r.getName()%></td>
<td><%=r.getPassword()%></td>
<td>
<a href="update.jsp?id=<%=r.getId()%>">修改</a>
<a href="delete.jsp?id=<%=r.getId()%>"
onclick="return confirm('确定删除?');">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
login.jsp
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>电子书--登陆</title>
<style type="text/css">
body
{
background:url(pictures/bg_read.jpg) no-repeat left top;
background-size:100%;
}
</style>
</head>
<div style="text-align:center;margin-top:140px">
<h1>爱阅读电子图书</h1>
<form action="LoginServlet" method="post">
<table style="margin-left:42%">
<marquee width="200"scrolldelay="250">Welcome!书籍是人类进步的阶梯!!!</marquee>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"></td>
</tr>
</table>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
<br>
<a href="add.jsp">注册</a>
</div>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>电子书</title>
<style type="text/css">
.fa-search {
height: 88px;
padding-top: 28px;
background: #fff;
border-bottom: 1px solid #c1bebd;
box-shadow: 0px 0px 3px #c1bebd;
}
body{
margin:0;
font-family:'微软雅黑','Times New Roman', Times, serif;
}
.navi_head{
height:50px;
background-color:#459df5;
}
.navi_body{
overflow:hidden;
height:50px;
background:rgba(36,97,158,0.9);
transition:height ease 0.5s;
}
.navi_body:hover{
height:300px;
}
.navi_head>div>span{
width:150px;
text-align:center;
height:300px;
display:inline-block;
font-weight:bold;
color:#FFF;
font-size:14px;
vertical-align:top;
}
.navi_head>div>span>p a{
color:#FFF;
text-decoration:none;
}
.navi_head>div>span>p a:hover{
color:#FFF;
text-decoration:underline;
}
.navi_title{
font-size:16px;
line-height:50px;
margin-top:0;
}
.navi_head>div>span:hover{
background:rgba(100,100,100,0.2);
}
</style>
</head>
<body>
<div><h1>爱阅读<h1></div>
<div>
<div class="navi_body">
<div class="navi_head">
<div style="width:80%; margin-left:auto; margin-right:auto;">
<span>
<p class="navi_title">首页</p>
</span>
<span>
<p class="navi_title">关于我们</p>
<p><a href="">团队介绍</a></p>
<p><a href="">服务QQ群</a></p>
<p><a href="">个人业务</a></p>
</span>
<span>
<p class="navi_title">软件下载</p>
<p><a href="">WEB编译工具</a></p>
<p><a href="">桌面整理工具</a></p>
</span>
<span>
<p class="navi_title">招贤纳士</p>
<p><a href="">WEB前端工程师</a></p>
<p><a href="">JAVAWEB工程师</a></p>
<p><a href="">APP开发工程师</a></p>
<p><a href="">数据库工程师</a></p>
<p><a href="">软件架构师</a></p>
<p><a href="">技术客服</a></p>
</span>
<span>
<p class="navi_title">给我留言</p>
<p><a href="">站内留言</a></p>
<p><a href="">站长信箱</a></p>
</span>
</div>
</div>
</div>
</div>
</body>
</html>
update.jsp
<%@ page language="java" import="com.example.ConnectDB" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'delete.jsp' starting page</title>
</head>
<body>
<form action="change.jsp" method="post">
<p>
修改读者:
</p>
SID:
<input type="text" name="id" value=<%=request.getParameter("id") %>>
<br>
<br>
姓名:
<input type="text" name="name">
<br>
<br>
年龄:
<input type="text" name="password">
<br>
<br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
效果图: