JDBC连接Oracle实现增、删、改操作
最近在做一个练习项目“在线考试系统”,在将整体架构搭好然后将任务分配给组员以后,自己完成自己部分的功能,在自己所完成的功能当初,涉及了一个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实现增、删、改操作的更多相关文章
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- C# 连接 Oracle数据库增删改查,事务
一. 前情提要 一般.NET环境连接Oracle数据库,是通过 TNS/SQL.NET 配置文件,而 TNS 必须要 Oracle 客户端(如果连接的是服务器的数据库,本地还要装一个 client , ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- JAVA通过JDBC连接Oracle数据库详解【转载】
JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- jdbc连接oracle数据库
/*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...
随机推荐
- 有两张表;使用SQL查询,查询所有的客户订单日期最新的前五条订单记录。
客户信息表(c CUSTOM)有以下字段:id.name.mobile 客户订单表(C_ORDER)有以下字段:id.custom_id.commodity.count.order _date Sel ...
- WebApplicationContext
在Web应用中,我们会用到WebApplicationContext 用它来保存上下文信息 那么它set到ServletContext的过程是怎么样呢 1)通过WEB.XML中监听类 p.p1 { ...
- easyui中的下拉菜单是树形结构时如何实现onchange方法
今天碰到一个问题就是我写的代码中的一个下拉列表显示的是树型菜单,代码如下(使用的是easyui): .... <tr> <td>地区:</td> <td> ...
- JVM 常用命令行工具
本文部分摘自<深入理解 Java 虚拟机第三版> 基础故障处理工具 Java 开发人员肯定都知道 JDK 的 bin 目录下有许多小工具,这些小工具除了用于编译和运行 Java 程序外,打 ...
- JavaCV FFmpeg AAC编码
上次成功通过FFmpeg采集麦克风的PCM数据,这次针对上一次的程序进行了改造,使用AAC编码采集后的数据. (传送门) JavaCV FFmpeg采集麦克风PCM音频数据 采集麦克风数据是一个解码过 ...
- java的多线程:线程安全问题
什么是线程安全? 为什么有线程安全问题? 当多个线程同时共享,同一个全局变量或静态变量,做写的操作时,可能会发生数据冲突问题,也就是线程安全问题.但是做读操作是不会发生数据冲突问题. 抢火车的例子: ...
- sql删除重复数据思路
总的思路就是先找出表中重复数据中的一条数据,插入临时表中,删除所有的重复数据,然后再将临时表中的数据插入表中.所以重点是如何找出重复数据中的一条数据,有三种情况 1.重复数据完全一样,使用distin ...
- halcon案例学习之cbm_label_simple
*cbm_label_simple 程序说明:*这个示例程序展示了如何使用基于组件的匹配来定位复合对象.在这种情况下,应该在图像中找到一个标签,用户既不知道其中的组件,也不知道它们之间的关系.因此,创 ...
- LRU缓存的实现
文章目录 LRU简介 LRU算法分析 实现代码 节点类 双向链表 LRUCache类 测试类 总结 LRU简介 LRU是"Least Recently Used"的简写,意思是最近 ...
- 编译安装 nginx -1.14.2
编译安装 nginx -1.14.2 1 ) 下载nginx-1.14.2 源码包: wget http://nginx.org/download/nginx-1.14.2.tar.gz 2 ) 编译 ...