首先,给一张截图:
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;
}
}
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;
}
}
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);
}
}
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 SelectAll() throws SQLException{
String sql = "select * from user";
List beans = qr.query(sql, new BeanListHandler(Bean.class));
return beans;
}
}
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);
}
}
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);
}
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);
}
}
com.mysql.jdbc.Driver jdbc:mysql://localhost:3307/xx root root 5 10名字可以改
jdbc:mysql://localhost:3307/xx 端口号,、xx这个数据局库名字,都可以改
内容挺多的,你可以参考着写
Insert title here id: 回到主页<%@ page language=“java” contentType=“text/html; charset=UTF-8”
charset=UTF-8"%>
编辑用户信息根据id
请输入要删除人的id:
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
用户名: | |
密码: | |
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
用户名: | |
密码: |
回到主页
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
最后,想测试的话,在这里有一个测试类,可写可不写:把注释恢复即可
还有,里面tomcat,servlet,d3p0等等,还需要你自己去安装和配置了!!!!!!
项目链接:https://pan.baidu.com/s/1WFoVLv1jyndxP9NzCmN6BA
提取码:lo8k