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