最近在做一个练习项目“在线考试系统”,在将整体架构搭好然后将任务分配给组员以后,自己完成自己部分的功能,在自己所完成的功能当初,涉及了一个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();
}
}
}
}
}

JDBC连接Oracle实现增、删、改操作的更多相关文章

  1. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. C# 连接 Oracle数据库增删改查,事务

    一. 前情提要 一般.NET环境连接Oracle数据库,是通过 TNS/SQL.NET 配置文件,而 TNS 必须要 Oracle 客户端(如果连接的是服务器的数据库,本地还要装一个 client , ...

  3. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  4. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据

    第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...

  5. JAVA通过JDBC连接Oracle数据库详解【转载】

    JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...

  6. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  7. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  8. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  9. jdbc连接oracle数据库

    /*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...

随机推荐

  1. 基于Let's Encrypt生成免费证书-支持多域名泛域名证书

    目录 客户端 certbot acme.sh 安装acme.sh 1. 自动安装 2. 手动安装 3. 测试收否安装成功 使用acme.sh生成证书 1. HTTP 方式 2. DNS 方式 1. 生 ...

  2. 「译」用 Blazor WebAssembly 实现微前端

    原文作者: Wael Kdouh 原文链接:https://medium.com/@waelkdouh/microfrontends-with-blazor-webassembly-b25e4ba3f ...

  3. maven 导出所有依赖jar到指定路径

    mvn dependency:copy-dependencies -DoutputDirectory=lib

  4. Spring Boot 2.x基础教程:多个文件的上传

    昨天,我们介绍了如何在Spring Boot中实现文件的上传.有读者问:那么如果有多个文件要同时上传呢?这就马上奉上,当碰到多个文件要同时上传的处理方法. 动手试试 本文的动手环节将基于Spring ...

  5. JButton的常用方法

    JButton 实现了普通的三态外加选中.禁用状态,有很多方法可以设置,不要自己去写鼠标监听器.setBorderPainted(boolean b)    //是否画边框,如果用自定义图片做按钮背景 ...

  6. sqlserver 清除表数据和拷贝表结构的操作

    最近在做一个ERP系统需要导入数据,因此用到了sql的一些操作,在这里记录一下. 1.清除表数据: Delete from 表名称 where XXX 2.拷贝表结构,需求是新增一个和某个表数据格式一 ...

  7. Solon rpc 之 SocketD 协议 - 消息上报模式

    Solon rpc 之 SocketD 协议系列 Solon rpc 之 SocketD 协议 - 概述 Solon rpc 之 SocketD 协议 - 消息上报模式 Solon rpc 之 Soc ...

  8. Spring中的@Valid 和 @Validated注解你用对了吗

    1.概述 本文我们将重点介绍Spring中 @Valid和@Validated注解的区别 . 验证用户输入是否正确是我们应用程序中的常见功能.Spring提供了@Valid和@Validated两个注 ...

  9. Desired_Capabilities配置

    appium服务器初始化参数 最全: https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps. ...

  10. 【Web】实现动态文章列表

    简单记录 -慕课网- 步骤二:动态文章列表效果 实现这个 一个网页中常见的文章列表效果. 怎么实现文章列表案例 分解一波,CSS来改变样式 标题标签 HTML的无序列表 去掉项目符号 符号所占空间 列 ...