SSM框架实现学校信息增删改查-第一节添加学校名称
大家好!第一次写文 语句可能有些生涩,看到我不足的地方可以给我留言~
好了,开始第一节,一个简单的添加学校名称功能~
可以先看一下前端html代码,运行程序看一些界面效果
超级简易的一个添加页面,首先我们要知道添加是根据学校id添加信息的,请看代码哦
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加</title>
<link rel="stylesheet" href="css/index.css" type="text/css" />
</head>
<body>
<form action="AddServlet" method="post">
<table border="1" class="t1">
<tr>
<td colspan="2"><h1>添加学校</h1></td>
</tr>
<tr>
<td>学校名称:</td>
<td><input type="text" name="schoolname"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
<input type="reset" value="清空"/>
</td>
</tr>
</table>
</form>
</body>
</html>
页面我们了解之后我们就可以写后台操作了
我们先创建一个pojo
package school.pojo;
public class School {
private int id;
private String schoolname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSchoolname() {
return schoolname;
}
public void setSchoolname(String schoolname) {
this.schoolname = schoolname;
}
}
取用户输入的学校名称Controller层
package school.controller.schoolservlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA.Request;
import school.pojo.School;
import school.pojo.Student;
import school.service.schoolservice.AddService;
public class AddServlet extends HttpServlet {
AddService addservice=new AddService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String schoolname=req.getParameter("schoolname");
School school =new School();
school.setSchoolname(schoolname);
addservice.add(school);
req.getRequestDispatcher("SelectServlet").forward(req, resp);
}
}
service层,把取到的数据传给到层
package school.service.schoolservice;
import school.dao.schooldao.AddDao;
import school.pojo.School;
import school.pojo.Student;
public class AddService {
AddDao adddao=new AddDao();
public void add(School school) {
// TODO Auto-generated method stub
adddao.add(school);
}
}
dao层进行数据库操作
package school.dao.schooldao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import login.utils.DbHelper;
import school.pojo.School;
import school.pojo.Student;
public class AddDao {
public void add(School school) {
// TODO Auto-generated method stub
//锟斤拷锟斤拷class.DbHelper锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截筹拷锟斤拷
System.out.println("连接mysql");
Connection conn= DbHelper.getConnection();
String sq1="INSERT INTO `school`(`schoolname`) VALUES (?)";
try {
PreparedStatement pst=conn.prepareStatement(sq1);
pst.setString(1, school.getSchoolname());
// pst.setString(2, school.getSchoolname());
int cout=pst.executeUpdate();
pst.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}
这样一个简单的添加学校名称就做好了