JSP连接MySQL的删除、修改、添加
1.在接口定义一个方法
public int deleteClass(int parseInt);
public int update(ClassInfo c);
public int insert(ClassInfo c);
2.实现类实现方法删除方法,修改,添加
。
// An highlighted block
public int deleteClass(int id) {
// TODO Auto-generated method stub
// 1.数据库连接
int result = 0;
Connection conn = Dome.getconn();
// 2.书写sql语句
String sql = "delete from classinfo where id=?";
// 3. 预编译sql
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 4.给占位符(参数)赋值
ps.setInt(1, id);
// 5.执行命令,删除
result = ps.executeUpdate();
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public int update(ClassInfo c) {
// TODO Auto-generated method stub
// 1.数据库连接
int result = 0;
Connection conn = Dome.getconn();
// 2.书写sql语句
String sql = "update classinfo set classname=? where id=?";
// 3. 预编译sql
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 4.
ps.setString(1, c.getClassname());
ps.setInt(2, c.getId());
// 5.
result = ps.executeUpdate();
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public int insert(ClassInfo c) {
// TODO Auto-generated method stub
// 1.数据库连接
int result = 0;
Connection conn = Dome.getconn();
// 2.书写sql语句
String sql = "insert into classinfo values(?,?)";
// 3. 预编译sql
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 4.给占位符(参数)赋值
ps.setInt(1, c.getId());
ps.setString(2, c.getClassname());
// 5.执行命令,删除
result = ps.executeUpdate();
//
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
3.创建实体层
package bao.du.entity;
public class ClassInfo {
/*班级实体类
*/
private int id;
private String classname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public ClassInfo(int id, String classname) {
this.id = id;
this.classname = classname;
}
public ClassInfo() {
}
public ClassInfo(String classname) {
this.classname = classname;
}
}
4.显示页面逻辑
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String id=request.getParameter("id");
IClassinfo icd=new ClassInfoDaoImpl();
int result =icd.deleteClass(Integer.parseInt(id));
if (result>0) {
response.sendRedirect("ClassServlet");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
int id=Integer.parseInt(request.getParameter("id"));
String classname=request.getParameter("classname");
String h=request.getParameter("h");
IClassinfo icd=new ClassInfoDaoImpl();
int result =0;
if (h.equals("")) {
result=icd.insert(new ClassInfo(id,classname));
} else {
result=icd.update(new ClassInfo(id,classname));
}
if (result>0) {
response.sendRedirect("ClassServlet");
}
}
}