概述
文章目录
- 1、写数据库
- 2、在pojo中写一个方法
- 3、操作实现
- 3.1在servelet中写一个相应的servlet方法
- 3.2删除的相关代码
- 3.3增加的相关代码servlet
- 3.4、编辑的相关代码
1、写数据库
2、在pojo中写一个方法
定义get,set,construct,tostring等方法
package com.situ.web.pojo;
public class Teacher {
private Integer id;
private String name;
private Integer age;
public Teacher() {
}
public Teacher(Integer id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private String address;
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", address='" + address + ''' +
'}';
}
}
3、操作实现
3.1在servelet中写一个相应的servlet方法
注意的是要进行继承httpservlet,写@webservlet ,并进行重写
为了保证输入的时候不会造成乱码,要在service 的首句加上req.setCharacterEncoding(“UTF-8”);
首先是实现了查询,也就是页面显示的代码
@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
System.out.println("TeacherServlet.service");
String method =req.getParameter("method");
if(method==null||method.equals("")){
method="selectAll";
} //这个是为了让他直接显示这个页面用的
switch (method){
case "selectAll":
select(req,resp);
break;
}
}
private void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet =null;
List<Teacher> list=new ArrayList<>();
try {
connection= JDBCUtil.getConnection();
String sql = "select id,name,age,address from teacher";
statement=connection.prepareStatement(sql);
System.out.println(statement);
resultSet = statement.executeQuery();
while(resultSet.next()){
int id=resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
Teacher teacher = new Teacher(id,name,age,address);
list.add(teacher);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.close(connection,statement,null);
}
//把list集合的数据放到request内存中
req.setAttribute("list",list);
//转发到studen_list.jsp页面进行展示
req.getRequestDispatcher("teacher_list.jsp").forward(req,resp);
}
}
3.2删除的相关代码
servlet中:
private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String id = req.getParameter("id");
Connection connection=null;
PreparedStatement statement = null;
try {
connection= JDBCUtil.getConnection();
String sql="delete from teacher where id=?";
statement = connection.prepareStatement(sql);
statement.setInt(1,Integer.parseInt(id));
System.out.println(statement);
int cout=statement.executeUpdate();
System.out.println(cout);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.close(connection,statement,null);
}
resp.sendRedirect(req.getContextPath()+"/teacher?method=selectAll"); //重定向,返回列表
}
teacher_list.jsp中
<table class="table table-hover">
<tr>
<td class="active">ID</td>
<td class="success">姓名</td>
<td class="warning">年纪</td>
<td class="info">地址</td>
<td class="danger">删除</td>
<td class="danger">修改</td>
</tr>
<%
for(Teacher teacher:list){
%>
<tr>
<td class="active"><%=teacher.getId()%></td>
<td class="success"><%=teacher.getName()%></td>
<td class="warning"><%=teacher.getAge()%></td>
<td class="info"><%=teacher.getAddress()%></td>
<td><a href="javascript:deleteById(<%=teacher.getId()%>)">删除</a></td>
<td><a>修改</a></td>
</tr>
<%
}%>
</table>
<script>
function deleteById(id){
var isDelete =confirm('您确定要删除吗?');
if(isDelete){
location.href='<%=request.getContextPath()%>/teacher?method=deleteById&id='+id;
}
}
</script>
3.3增加的相关代码servlet
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("TeacherServlet.add");
String name = req.getParameter("name");
String age =req.getParameter("age");
String address = req.getParameter("address");
Connection connection =null;
PreparedStatement statement = null;
try {
connection = JDBCUtil.getConnection();
String sql = "insert into teacher(name,age,address)value(?,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,Integer.parseInt(age));
statement.setString(3,address);
System.out.println(statement);
int cout=statement.executeUpdate();
System.out.println(cout);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.close(connection,statement,null);
}
resp.sendRedirect(req.getContextPath()+"/teacher?method=selectAll");
}
teacher_add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/teacher" method="post">
用户名:<input type="text" name="name"/><br/>
年龄:<input type="text" name="age"/><br/>
地址:<input type="text" name="address"/><br/>
<input type="hidden" name="method" value="add"> <br/>
<input type="submit" value="添加">
```
3.4、编辑的相关代码
servlet
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("TeacherServlet.update");
// update student set name=?,age=?,gender=? where id=16
String id = req.getParameter("id");
String name = req.getParameter("name");
String age = req.getParameter("age");
String address = req.getParameter("address");
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JDBCUtil.getConnection();
String sql = " update teacher set name=?,age=?,address=? where id=?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, Integer.parseInt(age));
statement.setString(3, address);
statement.setInt(4, Integer.parseInt(id));
System.out.println(statement);
int count = statement.executeUpdate();
System.out.println(count);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.close(connection, statement, null);
}
resp.sendRedirect(req.getContextPath() + "/teacher?method=selectAll");
}
private void getTeacherUpadtePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("TeacherServlet.getTeacherUpadtePage");
String id = req.getParameter("id");
Connection connection=null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Teacher teacher =null;
try {
connection = JDBCUtil.getConnection();
String sql = "select id,name,age,address from teacher where id=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,Integer.parseInt(id));
System.out.println(statement);
resultSet=statement.executeQuery();
if(resultSet.next()){
// int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
teacher = new Teacher(Integer.parseInt(id), name, age, address);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.close(connection,statement,null);
}
req.setAttribute("teacher",teacher); //将teacher封装,在jsp中用
req.getRequestDispatcher("teacher_update.jsp").forward(req,resp);
}
teacher_update.jsp
<%@ page import="com.situ.web.pojo.Teacher" %><%--
Created by IntelliJ IDEA.
User: dell
Date: 2022/7/21
Time: 22:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Teacher teacher = (Teacher) request.getAttribute("teacher");
%>
<form action="<%=request.getContextPath()%>/teacher?method=update" method="post">
<input type="hidden" name="id" value="<%=teacher.getId()%>"/>
用户名:<input type="text" name="name" value="<%=teacher.getName()%>"/><br/>
年龄:<input type="text" name="age" value="<%=teacher.getAge()%>"/><br/>
地址:<input type="text" name="address" value="<%=teacher.getAddress()%>"/><br/>
<input type="submit" value="修改"/>
</form>
</body>
</html>
最后
以上就是暴躁秋天为你收集整理的teacher 操作js版的全部内容,希望文章能够帮你解决teacher 操作js版所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复