搭建好项目记得导包:
连接池(根据自己的配置改):
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false
user=root
password=994994
连接数据库工具类:
package com.accp.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
public class BaseDao {
private static String driver;
private static String url;
private static String user;
private static String password;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
static {
Properties params = new Properties();
String configFile = "database.properties";
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
try {
params.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver=params.getProperty("driver");
url=params.getProperty("url");
user=params.getProperty("user");
password=params.getProperty("password");
}
//连接
public Connection getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭流
public void closeAll() {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//增删改
public int executeUpdate(String sql, Object... objects) {
getConnection();
int rows = 0;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i + 1, objects[i]);
}
rows = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll();
}
return rows;
}
//查询
public ResultSet executeQuery(String sql, List
实体类:
package com.accp.entity;
public class StudentInfo {
private Integer sId;
private String sName;
private String sGender;
private Integer sAge;
private String sAddress;
private String sEmail;
public Integer getsId() {
return sId;
}
public void setsId(Integer sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsGender() {
return sGender;
}
public void setsGender(String sGender) {
this.sGender = sGender;
}
public Integer getsAge() {
return sAge;
}
public void setsAge(Integer sAge) {
this.sAge = sAge;
}
public String getsAddress() {
return sAddress;
}
public void setsAddress(String sAddress) {
this.sAddress = sAddress;
}
public String getsEmail() {
return sEmail;
}
public void setsEmail(String sEmail) {
this.sEmail = sEmail;
}
}
Dao层:
package com.accp.dao;
import java.util.List;
import com.accp.entity.StudentInfo;
public interface StudentInfoDao {
public List select(StudentInfo si);
public int update(StudentInfo si);
public int delete(StudentInfo si);
}
Dao层Impl实现类:
package com.accp.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.accp.dao.BaseDao;
import com.accp.dao.StudentInfoDao;
import com.accp.entity.StudentInfo;
public class StudentInfoDaoImpl extends BaseDao implements StudentInfoDao{
@Override
public List select(StudentInfo si) {
// TODO Auto-generated method stub
String sql = "select * from studentInfo where 1=1";
List list = new ArrayList();
List
Service层:
package com.accp.service;
import java.util.List;
import com.accp.entity.StudentInfo;
public interface StudentInfoService {
public List select(StudentInfo si);
public int update(StudentInfo si);
public int delete(StudentInfo si);
}
Service层Impl实现类:
package com.accp.service.impl;
import java.util.List;
import com.accp.dao.StudentInfoDao;
import com.accp.dao.impl.StudentInfoDaoImpl;
import com.accp.entity.StudentInfo;
import com.accp.service.StudentInfoService;
public class StudentInfoServiceImpl implements StudentInfoService{
StudentInfoDao sid = new StudentInfoDaoImpl();
@Override
public List select(StudentInfo si) {
// TODO Auto-generated method stub
return sid.select(si);
}
@Override
public int update(StudentInfo si) {
// TODO Auto-generated method stub
return sid.update(si);
}
@Override
public int delete(StudentInfo si) {
// TODO Auto-generated method stub
return sid.delete(si);
}
}
Servlet层删除:
package com.accp.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.accp.dao.impl.StudentInfoDaoImpl;
import com.accp.entity.StudentInfo;
/**
* Servlet implementation class DeleteStudentInfoServlet
*/
@WebServlet("/dsis")
public class DeleteStudentInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteStudentInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String id = request.getParameter("id"); //获得要删除的用户ID
StudentInfo si = new StudentInfo();
si.setsId(Integer.valueOf(id));
StudentInfoDaoImpl sis = new StudentInfoDaoImpl();
int count = sis.delete(si); //调用删除方法
//写入流
PrintWriter out = response.getWriter();
out.print(count);
out.flush();
out.close();
}
/**
* @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);
}
}
Servlet层修改:
package com.accp.servlet;
import java.io.IOException;
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.accp.dao.impl.StudentInfoDaoImpl;
import com.accp.entity.StudentInfo;
/**
* Servlet implementation class UpdateStudentInfoServlet
*/
@WebServlet("/usis")
public class UpdateStudentInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateStudentInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
/*
* 获得修改后的学员信息
*/
String id = request.getParameter("id");
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
String gender = request.getParameter("gender");
gender = new String(gender.getBytes("ISO-8859-1"), "UTF-8");
String age = request.getParameter("age");
String address = request.getParameter("address");
address = new String(address.getBytes("ISO-8859-1"), "UTF-8");
String email = request.getParameter("email");
// 申明学员对象存入值
StudentInfo si = new StudentInfo();
si.setsId(Integer.valueOf(id));
si.setsName(name);
si.setsGender(gender);
si.setsAge(Integer.valueOf(age));
si.setsAddress(address);
si.setsEmail(email);
StudentInfoDaoImpl sis = new StudentInfoDaoImpl();
// 调用修改方法
int count = sis.update(si);
// 修改后提示
if (count > 0) {
response.getWriter().write("");
} else {
response.getWriter().write("");
}
}
/**
* @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);
}
}
Servlet层查询:
package com.accp.servlet;
import java.io.IOException;
import java.util.List;
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.accp.dao.impl.StudentInfoDaoImpl;
import com.accp.entity.StudentInfo;
/**
* Servlet implementation class SelectStudentInfoServlet
*/
@WebServlet("/ssis")
public class SelectStudentInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectStudentInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
StudentInfo si = new StudentInfo();
StudentInfoDaoImpl sis = new StudentInfoDaoImpl();
List list = sis.select(si); //调用查询方法
request.setAttribute("list",list); //存入作用域
//转发
request.getRequestDispatcher("index.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);
}
}
Servlet层传值到修改页面查看:
package com.accp.servlet;
import java.io.IOException;
import java.util.List;
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.accp.dao.impl.StudentInfoDaoImpl;
import com.accp.entity.StudentInfo;
/**
* Servlet implementation class SelectStudentInfoByIdServlet
*/
@WebServlet("/ssibis")
public class SelectStudentInfoByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectStudentInfoByIdServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String id = request.getParameter("id"); //获得要修改的学员ID
StudentInfo si = new StudentInfo();
si.setsId(Integer.valueOf(id));
StudentInfoDaoImpl sis = new StudentInfoDaoImpl();
List list = sis.select(si); //通过ID去查询该学员信息
request.setAttribute("info", list.get(0)); //将信息存入作用域
//转发
request.getRequestDispatcher("upd.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);
}
}
显示页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
学员信息列表
学员编号
学员姓名
学员性别
学员年龄
家庭住址
Email
操作
${item.sId}
${item.sName}
${item.sGender}
${item.sAge}
${item.sAddress}
${item.sEmail}
删除
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here