import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JdbcUpdate { /**
* @param args
*/
public static void main(String[] args) {
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//获取连接
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.16.111:1521:orcl","scott","tiger");
//获得句柄
Statement sta = con.createStatement(); //SQL查询命令
String sqlFind = "select * from empbak";
//从emp中复制一个新表为empbak
String sqlCreTable = "create table empbak as select * from emp";
//NVL (expr1, expr2):expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一
String sqlUpdate = "update empbak set sal = sal+200,comm = nvl(comm+100,100) where sal > (select avg(sal) from empbak )";
//复制表
int temp = sta.executeUpdate(sqlCreTable); //初始查询
//发送SQL语句,如果发送的是select,使用executeQuery;如果发送的是INSERT,DELETE,UPDATE,使用executeUpdate
ResultSet rs1 = sta.executeQuery(sqlFind);
while(rs1.next()){
System.out.print(rs1.getInt("empno")+"\t");
System.out.print(rs1.getString("ename")+"\t");
System.out.print(rs1.getString("job")+"\t");
System.out.print(rs1.getInt("mgr")+"\t");
System.out.print(rs1.getString("hiredate")+"\t");
System.out.print(rs1.getFloat("sal")+"\t");
System.out.print(rs1.getFloat("comm")+"\t");
System.out.println();
}
System.out.println("********************更新以后*********************");
int temp1 = sta.executeUpdate(sqlUpdate);
ResultSet rs = sta.executeQuery(sqlFind);
while(rs.next()){
System.out.print(rs.getInt("empno")+"\t");
System.out.print(rs.getString("ename")+"\t");
System.out.print(rs.getString("job")+"\t");
System.out.print(rs.getInt("mgr")+"\t");
System.out.print(rs.getString("hiredate")+"\t");
System.out.print(rs.getFloat("sal")+"\t");
System.out.print(rs.getFloat("comm")+"\t");
System.out.println();
}
rs.close();
sta.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }

最新文章

  1. CocoaPods的使用及安装
  2. 《C标准库》——之<string.h>
  3. OA及权限系统
  4. SQL SA密码丢失
  5. WEB 安全之 SQL注入 < 二 > 暴库
  6. 【转载】mysql 四种隔离级别分析
  7. How to use Android Activity's finish(), onDestory() and System.exit(0) methods
  8. Swift与Objective-C的对比
  9. IBM即将倒闭,微软也从崩溃18个月
  10. python day two,while
  11. css坑了我一下下之line-height
  12. 基本 SQL 之增删改查(一)
  13. sed 删除文本
  14. Linux中普通用户配置sudo权限(带密或免密)
  15. Android CameraManager 类
  16. 如果往错误的NEO地址转账会发生什么
  17. [linux]解析crontab
  18. 服务器上定时自动执行php
  19. zabbix3.4.7版本饼图显示问题
  20. DataGuard 配置须知

热门文章

  1. C语言笔记(二)
  2. C#中SetWindowPos函数详解
  3. Apache_安装
  4. kafkaAPI
  5. 爬虫写法进阶:普通函数--->函数类--->Scrapy框架
  6. Golang - 复合类型
  7. android 手机网络接入点名称及WAP、NET模式的区别
  8. 多层下firebird自增长字段的处理
  9. firefox历史版本下载地址
  10. SQL SERVER高级知识积累