首先,给一张截图:
上面图是我的整体内容
1、先进入工具类
代码如下:package com.qf.util;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Util {
public static DataSource ds = null;
static{
ds = new ComboPooledDataSource("mypool");
}
public static DataSource getConnection(){
return ds;
}
}
2、
package com.qf.bean;
public class Bean {
private int id;
private String username;
private String password;
public Bean() {
super();
// TODO Auto-generated constructor stub
}
public Bean(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;
}
}
3、
package com.qf.del;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.jdbcserver.Server;
/**
-
Servlet implementation class Del
*/
public class Del extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Server server = new Server();
RequestDispatcher dispatcher = null;
int row = 0;
int id = Integer.valueOf(request.getParameter(“id”)).intValue();
try {
row = server.DelUser(id);
if(row != 0){
dispatcher = request.getRequestDispatcher("/del.html");
}else{
dispatcher = request.getRequestDispatcher("/error");
}
dispatcher.forward(request, response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4、
package com.qf.jdbcserver;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.qf.bean.Bean;
import com.qf.util.Util;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class Server {
private QueryRunner qr = new QueryRunner(Util.getConnection());
// private final String TABLE_NAME = “user”;
private int row = 0;
public Bean Login(String username,String password) throws SQLException{
Bean user = null;
String sql = "select * from user where username = ? and password = ? ";
user = qr.query(sql,new BeanHandler(Bean.class),username,password);
return user;
}
public int Insert(Object username,Object password) throws SQLException{
String sql = "insert into user values(null,?,?)";
row =qr.update(sql, username,password);
return row;
}
public int DelUser(int id) throws SQLException{
String sql = "delete from user where id =?";
row = qr.update(sql, id);
return row;
}
//改:根据id改密码
public int UpdateUser(int id,String username,String password) throws SQLException{
String sql ="update user set username= ? , password = ? where id = ?";
row = qr.update(sql,username, password,id);
return row;
}
public List<Bean> SelectAll() throws SQLException{
String sql = "select * from user";
List<Bean> beans = qr.query(sql, new BeanListHandler<Bean>(Bean.class));
return beans;
}
}
5、
package com.qf.register;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;
/**
-
Servlet implementation class RegisterServlet
*/
public class RegisterServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);
Server server = new Server();
RequestDispatcher dispatcher =null;
int row = 0;
String username =request.getParameter(“username”);
String password = request.getParameter(“password”);
try {
row=server.Insert(username, password);
if(row != 0){
dispatcher = request.getRequestDispatcher("/login.jsp");
}else{
dispatcher = request.getRequestDispatcher("/error.jsp");
}
dispatcher.forward(request, response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
6、
package com.qf.selectall_information;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;
/**
-
Servlet implementation class SelectInformation
*/
public class SelectInformation extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Server server = new Server();
List user= null;
RequestDispatcher dispatcher = null ;
System.out.println(“1”);
try {
System.out.println(“2”);
user= server.SelectAll();
if(user != null){
for (Bean u : user) {
System.out.println(“用户名ID:”+u.getId()+"\t “+“用户名:”+u.getUsername()+”\t"+“密码:”+u.getPassword());
}
dispatcher=request.getRequestDispatcher("/information.html");
}else{
dispatcher = request.getRequestDispatcher("/error.jsp");
}
dispatcher.forward(request, response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
7、
package com.qf.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;
/**
-
Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);
Server server = new Server();
RequestDispatcher rd = null;
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);Bean user = null; try { user = server.Login(username, password); if(user!=null){ rd=request.getRequestDispatcher("/zhuye.html"); }else{ rd=request.getRequestDispatcher("/register.jsp"); } rd.forward(request, response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
8、
package com.qf.update;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.jdbcserver.Server;
import com.sun.rowset.internal.InsertRow;
/**
-
Servlet implementation class Update
*/
public class Update extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Server server = new Server();
RequestDispatcher dispatcher = null;
int id =Integer.valueOf(request.getParameter(“id”)).intValue();
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
int row = 0;try { row = server.UpdateUser(id, username, password); if(row != 0){ dispatcher = request.getRequestDispatcher("/update.html"); }else{ dispatcher =request.getRequestDispatcher("/error.jsp"); } dispatcher.forward(request, response); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
9、
<?xml version="1.0" encoding="UTF-8"?> com.mysql.jdbc.Driver jdbc:mysql://localhost:3307/xx root root 5 10名字可以改
jdbc:mysql://localhost:3307/xx 端口号,、xx这个数据局库名字,都可以改
10、
<?xml version="1.0" encoding="UTF-8"?> project_3 LoginServlet com.qf.servlet.LoginServlet LoginServlet /login RegisterServlet com.qf.register.RegisterServlet RegisterServlet /register SelectInformation com.qf.selectall_information.SelectInformation SelectInformation /See Update com.qf.update.Update Update /update Del com.qf.del.Del Del /del内容挺多的,你可以参考着写
11、
12、
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
charset=UTF-8"%>
你的信息有误,请检查你的错误
13、
欢迎来到员工管理界面,请注意点击哦,不要做不正当行为
员工所有信息
进入员工的信息库 <a href="update.html"><h3>编辑用户信息根据id</h3></a>
<a href=" del.html"><h2>请输入要删除人的id:</h2></a>
14、
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
欢迎来到登录界面,请登录
</tr>
</table>
</form>
用户名: | |
密码: | |
15、
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
欢迎来到注册界面,请注册:
<tr>
<td><input type="submit" name="submit" id="submit" value="提交" /></td>
</tr>
</table>
</form>
用户名: | |
密码: |
16、
跳到员工管理界面
17、
修改信息
ID: 用户名: 密码: <a href="zhuye.html">回到主页</a>
18、
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
修改信息
ID: 用户名: 密码:19、
最后,想测试的话,在这里有一个测试类,可写可不写:把注释恢复即可
还有,里面tomcat,servlet,d3p0等等,还需要你自己去安装和配置了!!!!!!
项目链接:https://pan.baidu.com/s/1WFoVLv1jyndxP9NzCmN6BA
提取码:lo8k