JDBC连接Oracle实现增、删、改操作
2024-10-19 02:14:53
最近在做一个练习项目“在线考试系统”,在将整体架构搭好然后将任务分配给组员以后,自己完成自己部分的功能,在自己所完成的功能当初,涉及了一个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();
}
}
}
}
}
最新文章
- ArcGIS Engine开发前基础知识(3)
- NYOJ:题目860 又见01背包
- 在Visual Studio中使用Pseudovariables来帮助调试
- hadoop 异常 INFO ipc.Client: Retrying connect to server:
- zongjie
- [转]ef使用dbfirst方式连接mysql
- Git配合Tag的代码回滚
- 程序员必须知道的六大ES6新特性
- 30个CSS3选择器的应用
- redis 突然大量逐出导致读写请求block
- Tomcat 目录结构以及基本配置
- Google - Find Most People in Chat Log
- Qt_深入了解信号槽(signal&;slot)
- Docker实现原理之Namespace,CGroup
- 如何让xcode自动检查内存泄露
- C#编程(十二)----------函数
- 包含.h就可以用其对应的函数
- BZOJ1197 [HNOI2006]花仙子的魔法
- 【转】 关于寄存器ESP和EBP的一些理解
- Unity教程之-Unity3d中针对Android Apk的签名验证(C#实现)