dao.java
package Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import DBUtil.Course;
import DBUtil.DBUtil;
import DBUtil.DBUtil;
public class dao
{
//添加
//@param course
public boolean add(Course course)
{
String sql="insert into class(name,grade) values('"+course.getName()+"','"+course.getGrade()+"')";
Connection conn=DBUtil.getConn();//数据库连接,加载驱动
Statement state=null;//数据库操作
try
{
state=conn.createStatement();//实例化Statement对象
state.executeUpdate(sql);//执行数据库更新操作
}catch(Exception e)//当try语句中出现异常时,会执行catch中的语句
{
e.printStackTrace();//捕获异常的语句
}
finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
{
DBUtil.close(state, conn); //close释放资源
}
return false;
}
//删除
//@param id
public boolean delete(int id)
{
String sql="delete from class where id='"+id+"'";
Connection conn=DBUtil.getConn();
Statement state=null;
try
{
state=conn.createStatement();
state.executeUpdate(sql);//价格问号??为什么原版是a=state.executeUpdate(sql),而只有添加那里没有
}catch (Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(state, conn);
}
return false;
}
//修改
//@param name
public boolean update(Course course)
{
String sql="update class set name='"+course.getName()+"',place='"+course.getGrade()+"'where id='"+course.getId()+"'";
Connection conn=DBUtil.getConn();
Statement state=null;
try
{
state=conn.createStatement();
state.executeUpdate(sql);
}catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(state, conn);
}
return false;
}
/**
* 验证人物名称是否唯一
* true --- 不唯一
* @param name
* @return
*/
public boolean name(String name) {
boolean flag = false;
String sql = "select name from class where name = '" + name + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
/**
* 通过ID得到类
* @param id
* @return
*/
public Course getCourseById(int id) {
String sql = "select * from class where id ='" + id + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
Course course = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
int grade = rs.getInt("grade");
course = new Course(id, name, grade);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return course;
}
//通过name得到Course
//@param name
public Course getCourseByName(String name)
{
boolean f=false;
String sql="select name from class where name='"+name+"'";
Connection conn=DBUtil.getConn(); //Connection是建立与数据库的链接
Statement state=null; //Statement算是一个连接的实例,用来执行SQL语句,
ResultSet rs=null; //ResultSet是查询后得到的结果集,得到结果后必须执行.next()方法
Course course = null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
int grade = rs.getInt("grade");
course = new Course(id, name,grade);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs,state, conn);
}
return course;
}
//查找
//@param name
public List<Course>search(String name,int grade)
{
String sql="select * from class where";
if (name!=" ")
{
sql+="name like'%"+name+"%'";
}
if (grade!=' ')
{
sql+="name like'%"+grade+"%'";
}
List<Course>list=new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean=null;
while (rs.next())
{
int id=rs.getInt("id");
String name1=rs.getString("name");
int grade1=rs.getInt("grade");
bean=new Course(id,name1,grade1);
list.add(bean); }
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
{
//添加
//@param course
public boolean add(Course course)
{
String sql="insert into class(name,grade) values('"+course.getName()+"','"+course.getGrade()+"')";
Connection conn=DBUtil.getConn();//数据库连接,加载驱动
Statement state=null;//数据库操作
try
{
state=conn.createStatement();//实例化Statement对象
state.executeUpdate(sql);//执行数据库更新操作
}catch(Exception e)//当try语句中出现异常时,会执行catch中的语句
{
e.printStackTrace();//捕获异常的语句
}
finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
{
DBUtil.close(state, conn); //close释放资源
}
return false;
}
//删除
//@param id
public boolean delete(int id)
{
String sql="delete from class where id='"+id+"'";
Connection conn=DBUtil.getConn();
Statement state=null;
try
{
state=conn.createStatement();
state.executeUpdate(sql);//价格问号??为什么原版是a=state.executeUpdate(sql),而只有添加那里没有
}catch (Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(state, conn);
}
return false;
}
//修改
//@param name
public boolean update(Course course)
{
String sql="update class set name='"+course.getName()+"',place='"+course.getGrade()+"'where id='"+course.getId()+"'";
Connection conn=DBUtil.getConn();
Statement state=null;
try
{
state=conn.createStatement();
state.executeUpdate(sql);
}catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(state, conn);
}
return false;
}
/**
* 验证人物名称是否唯一
* true --- 不唯一
* @param name
* @return
*/
public boolean name(String name) {
boolean flag = false;
String sql = "select name from class where name = '" + name + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
/**
* 通过ID得到类
* @param id
* @return
*/
public Course getCourseById(int id) {
String sql = "select * from class where id ='" + id + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
Course course = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
int grade = rs.getInt("grade");
course = new Course(id, name, grade);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return course;
}
//通过name得到Course
//@param name
public Course getCourseByName(String name)
{
boolean f=false;
String sql="select name from class where name='"+name+"'";
Connection conn=DBUtil.getConn(); //Connection是建立与数据库的链接
Statement state=null; //Statement算是一个连接的实例,用来执行SQL语句,
ResultSet rs=null; //ResultSet是查询后得到的结果集,得到结果后必须执行.next()方法
Course course = null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
int grade = rs.getInt("grade");
course = new Course(id, name,grade);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs,state, conn);
}
return course;
}
//查找
//@param name
public List<Course>search(String name,int grade)
{
String sql="select * from class where";
if (name!=" ")
{
sql+="name like'%"+name+"%'";
}
if (grade!=' ')
{
sql+="name like'%"+grade+"%'";
}
List<Course>list=new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean=null;
while (rs.next())
{
int id=rs.getInt("id");
String name1=rs.getString("name");
int grade1=rs.getInt("grade");
bean=new Course(id,name1,grade1);
list.add(bean); }
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
//全部数据
public List<Course>list()
{
String sql="select * from course";
List<Course>list=new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean=null;
while (rs.next())
{
int id=rs.getInt("id");
String name=rs.getString("name");
int grade=rs.getInt("grade");
bean=new Course(id,name,grade);
}
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
}
//全部数据
public List<Course>list()
{
String sql="select * from course";
List<Course>list=new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean=null;
while (rs.next())
{
int id=rs.getInt("id");
String name=rs.getString("name");
int grade=rs.getInt("grade");
bean=new Course(id,name,grade);
}
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
}
Course.java
package DBUtil;
public class Course
{
private int id;
private String name;
private int grade;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
public Course(int id, String name, int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Course(String name, int grade) {
this.name = name;
this.grade = grade;
}
}
{
private int id;
private String name;
private int grade;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
public Course(int id, String name, int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Course(String name, int grade) {
this.name = name;
this.grade = grade;
}
}
DBUtil.java
package DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库连接工具
* @author Hu
*
*/
public class DBUtil {
//联结字符串 //数据库名test
public static String db_url = "jdbc:mysql://localhost:3306/test?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//数据库用户名
public static String db_user = "root";
//数据库密码名
public static String db_pass = "root";
public static Connection getConn () {
//声明与数据库的连接并实例化为null
Connection conn = null;
try {
//驱动程序名
Class.forName("com.mysql.cj.jdbc.Driver");//连接数据库
//具体地连接到数据库——联接字符串(数据库名),联接用户名,联接密码名
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
* 数据库连接工具
* @author Hu
*
*/
public class DBUtil {
//联结字符串 //数据库名test
public static String db_url = "jdbc:mysql://localhost:3306/test?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//数据库用户名
public static String db_user = "root";
//数据库密码名
public static String db_pass = "root";
public static Connection getConn () {
//声明与数据库的连接并实例化为null
Connection conn = null;
try {
//驱动程序名
Class.forName("com.mysql.cj.jdbc.Driver");//连接数据库
//具体地连接到数据库——联接字符串(数据库名),联接用户名,联接密码名
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
servlet.java
package Servlet;
import java.io.IOException;
import java.util.List;
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 javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DBUtil.Course;
import Dao.service;
import Dao.service;
@WebServlet("/servlet")
public class servlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
service service1=new service();
//方法选择
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException
{
req.setCharacterEncoding("utf-8");//设置从jsp中请求道德数据的值,也就是设置为中文,防止乱码
String method=req.getParameter("method");//getParameter()获取的是客户端设置的数据。
if ("add".equals(method)) {
add(req, resp);
} else if ("del".equals(method)) {
del(req, resp);
} else if ("update".equals(method)) {
update(req, resp);
} else if ("search".equals(method)) {
search(req, resp);
} else if ("getcoursebyid".equals(method)) {
getCourseById(req, resp);
} else if ("getcoursebyname".equals(method)) {
getCourseByName(req, resp);
} else if ("list".equals(method)) {
list(req, resp);
}
}
//添加
public class servlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
service service1=new service();
//方法选择
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException
{
req.setCharacterEncoding("utf-8");//设置从jsp中请求道德数据的值,也就是设置为中文,防止乱码
String method=req.getParameter("method");//getParameter()获取的是客户端设置的数据。
if ("add".equals(method)) {
add(req, resp);
} else if ("del".equals(method)) {
del(req, resp);
} else if ("update".equals(method)) {
update(req, resp);
} else if ("search".equals(method)) {
search(req, resp);
} else if ("getcoursebyid".equals(method)) {
getCourseById(req, resp);
} else if ("getcoursebyname".equals(method)) {
getCourseByName(req, resp);
} else if ("list".equals(method)) {
list(req, resp);
}
}
//添加
private void add(HttpServletRequest req,HttpServletResponse resp)throws IOException, ServletException
{
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
Course course=new Course(name,grade);
//添加后消息提示
if(service1.add(course))
{
req.setAttribute("massage", "添加成功");
req.getRequestDispatcher("add.jsp").forward(req, resp);
}
else
{
req.setAttribute("massage", "名称重复,请重新录入");
req.getRequestDispatcher("add.jsp").forward(req, resp);//处理完了,分发到下一个JSP页面或者下一个Action继续处理。
}
}
//全部
private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
List<Course> courses = service1.list();
req.setAttribute("courses", courses);
req.getRequestDispatcher("list.jsp").forward(req,resp);
}
//通过ID得到Course, 转到detail2.jsp
private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
Course course = service1.getCourseById(id);
req.setAttribute("course", course);
req.getRequestDispatcher("detail2.jsp").forward(req,resp);
}
//通过name查找Course, 转到detail1.jsp
private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
Course course = service1.getCourseByName(name);
if(course == null) {
req.setAttribute("message", "查无此人");
req.getRequestDispatcher("del.jsp").forward(req,resp);
} else {
req.setAttribute("course", course);
req.getRequestDispatcher("detail.jsp").forward(req,resp);
}
}
//删除delete
private void del(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
req.setCharacterEncoding("UTF-8");
int id=Integer.parseInt(req.getParameter("id"));
service1.del(id);
req.setAttribute("message", "删除成功");
req.getRequestDispatcher("del.jsp").forward(req, resp);
}
//修改update
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
Course course = new Course(id, name,grade);
service1.update(course);
req.setAttribute("message", "修改成功");
req.getRequestDispatcher("servlet?method=list").forward(req,resp);
//?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
}
//查询
private void search(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
List<Course>courses=service1.search(name, grade);
req.setAttribute("Course", courses);
req.getRequestDispatcher("searchlist.jsp").forward(req, resp);
}
}
{
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
Course course=new Course(name,grade);
//添加后消息提示
if(service1.add(course))
{
req.setAttribute("massage", "添加成功");
req.getRequestDispatcher("add.jsp").forward(req, resp);
}
else
{
req.setAttribute("massage", "名称重复,请重新录入");
req.getRequestDispatcher("add.jsp").forward(req, resp);//处理完了,分发到下一个JSP页面或者下一个Action继续处理。
}
}
//全部
private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
List<Course> courses = service1.list();
req.setAttribute("courses", courses);
req.getRequestDispatcher("list.jsp").forward(req,resp);
}
//通过ID得到Course, 转到detail2.jsp
private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
Course course = service1.getCourseById(id);
req.setAttribute("course", course);
req.getRequestDispatcher("detail2.jsp").forward(req,resp);
}
//通过name查找Course, 转到detail1.jsp
private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
Course course = service1.getCourseByName(name);
if(course == null) {
req.setAttribute("message", "查无此人");
req.getRequestDispatcher("del.jsp").forward(req,resp);
} else {
req.setAttribute("course", course);
req.getRequestDispatcher("detail.jsp").forward(req,resp);
}
}
//删除delete
private void del(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
req.setCharacterEncoding("UTF-8");
int id=Integer.parseInt(req.getParameter("id"));
service1.del(id);
req.setAttribute("message", "删除成功");
req.getRequestDispatcher("del.jsp").forward(req, resp);
}
//修改update
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
Course course = new Course(id, name,grade);
service1.update(course);
req.setAttribute("message", "修改成功");
req.getRequestDispatcher("servlet?method=list").forward(req,resp);
//?method=list表示传一个叫做method的参数,他的值是list,你可以在Servlet中用request.getParam...获取到。
}
//查询
private void search(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
int grade=Integer.parseInt(req.getParameter("grade"));
List<Course>courses=service1.search(name, grade);
req.setAttribute("Course", courses);
req.getRequestDispatcher("searchlist.jsp").forward(req, resp);
}
}
结束。
这次挑战赛失败,不过没关系,一点一点地把程序骂了出来并且做了详细的注释。也算颇丰。