最近在做一个练习项目“在线考试系统”,在将整体架构搭好然后将任务分配给组员以后,自己完成自己部分的功能,在自己所完成的功能当初,涉及了一个jsp页面写入数据,将数据提交保存至数据库,可对数据实现增、删、改、查等操作,将其中主要功能实现以作总结。


jsp页面的form表单

<form method="post" action="AddMent.action">
<input name="deptid" type="text">
<input type="text" name="deptid" placeholder="请输入部门编号" />
<input type="text" name="deptname" placeholder="请输入部门名称" />
<input type="text" name="deptnews" placeholder="请输入部门信息" />
<button class="button" type="submit"> 提交</button>

AddMent.action实现向数据新增数据操作

@WebServlet(value="/System/AddMent.action")
public class AddMentDao extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
/* String uname = req.getParameter("uname");
String qx = req.getParameter("qx");*/
Connection con=null;
Statement st=null;
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.建立数据库连接
con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
//3.获取执行sql语句的平台
st= con.createStatement();
String deptid = req.getParameter("deptid");
String deptname = req.getParameter("deptname");
String deptnew = req.getParameter("deptnew");
//4.执行sql语句插入数据
String sql = "INSERT INTO dept(deptid, deptname, deptnew) "
+ " VALUES(?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, req.getParameter("deptid"));
pstmt.setString(2, req.getParameter("deptname"));
pstmt.setString(3, req.getParameter("deptnew"));
pstmt.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭st
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭con
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

DeleteMent删除操作

@WebServlet(value="/System/Delete.Action")
public class DeleteMent extends HttpServlet{
/**
* 添加试题
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String deptname = req.getParameter("deptname");
Connection con=null;
Statement st=null;
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.建立数据库连接
con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
//3.获取执行sql语句的平台
st= con.createStatement();
//4.执行sql语句插入数据
String sql = "DELETE FROM dept WHERE deptname=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, req.getParameter("deptname"));
pstmt.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭st
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭con
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

UpdateMent修改操作

@WebServlet(value="/System/Update.action")
public class UpdateMent extends HttpServlet{
/**
* 添加试题
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
Connection con=null;
Statement st=null;
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.建立数据库连接
con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
//3.获取执行sql语句的平台
st= con.createStatement();
String deptid = req.getParameter("deptid");
String deptname = req.getParameter("deptname");
String deptnew = req.getParameter("deptnew");
String sql="update dept set deptid=?,deptname=?,deptnew=?"+
"where deptid=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, req.getParameter("deptid"));
pstmt.setString(2, req.getParameter("deptname"));
pstmt.setString(3, req.getParameter("deptnew"));
pstmt.setString(4, req.getParameter("deptid"));
pstmt.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭st
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭con
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

最新文章

  1. ArcGIS Engine开发前基础知识(3)
  2. NYOJ:题目860 又见01背包
  3. 在Visual Studio中使用Pseudovariables来帮助调试
  4. hadoop 异常 INFO ipc.Client: Retrying connect to server:
  5. zongjie
  6. [转]ef使用dbfirst方式连接mysql
  7. Git配合Tag的代码回滚
  8. 程序员必须知道的六大ES6新特性
  9. 30个CSS3选择器的应用
  10. redis 突然大量逐出导致读写请求block
  11. Tomcat 目录结构以及基本配置
  12. Google - Find Most People in Chat Log
  13. Qt_深入了解信号槽(signal&amp;slot)
  14. Docker实现原理之Namespace,CGroup
  15. 如何让xcode自动检查内存泄露
  16. C#编程(十二)----------函数
  17. 包含.h就可以用其对应的函数
  18. BZOJ1197 [HNOI2006]花仙子的魔法
  19. 【转】 关于寄存器ESP和EBP的一些理解
  20. Unity教程之-Unity3d中针对Android Apk的签名验证(C#实现)

热门文章

  1. Go之获取系统性能指标 - goPsutil
  2. JAVA编程能力提升学习图
  3. 【转载】VUE的背景图引入
  4. Linux系统性能排查
  5. hdp、cdh版本大数据maven仓库
  6. 查找linux系统下的端口被占用进程的两种方法 【转】
  7. halcon案例学习之cbm_label_simple
  8. 【Java基础】Eclipse 和数组
  9. 【Linux】md5sum 生产所有文件的md5值,并对照目标文件是否相同
  10. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)