Oracle存储过程实例
来源:互联网 发布:js报表控件 编辑:程序博客网 时间:2024/06/03 15:54
1存储过程
create or replace procedure addBook1(bNin varchar2,tdin number,n1out number,n2out number) as
begin
declare maxId number;
n number;
begin
select count(*) into n1from t_book where bookName=bn;
select count(*) into nfrom t_book where bookName=bn;
if(n<2)then
return;
end if;
select max(id) into maxIdfrom t_book where bookName=bn;
delete from t_bookwhere id < maxId and bookName = bn;
select count(*) into n2from t_book where bookName=bn;
commit;
end;
end addBook1;
2.java调用实例
Test测试类
package test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
public class Test {
/**
* 一个非常标准的连接Oracle数据库的示例代码
*/
public void testOracle()
{
Connection con = null;// 创建一个数据库连接
CallableStatement pre = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");//加载Oracle驱动程序
System.out.println("开始尝试连接数据库!");
String url = "jdbc:oracle:thin:@localhost:1521:TEST";// 127.0.0.1是本机地址,TEST是精简版Oracle的默认数据库名
String user = "shopping";// 用户名,系统默认的账户名
String password = "123456";// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);//获取连接
System.out.println("连接成功!");
String sql = "{call addBook1(?,?,?,?)}";
pre = con.prepareCall(sql);// 实例化预编译语句
pre.setString(1, "大熊");//设置参数,前面的1表示参数的索引,而不是表中列名的索引
pre.setInt(2, 2);
pre.registerOutParameter(3, Types.INTEGER); //给返回的值申明变量
pre.registerOutParameter(4, Types.INTEGER);
pre.execute();//执行查询
int n1 = pre.getInt(3);
int n2 = pre.getInt(4);
System.out.println(n1);
System.out.println(n2);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (pre != null)
pre.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Main方法调用
package test;
public class Test1 {
public static void main(String[]args) {
Test test = new Test();
test.testOracle();
}
}
- Oracle 存储过程实例
- Oracle存储过程实例
- oracle 存储过程实例
- oracle存储过程实例
- Oracle存储过程实例
- Oracle存储过程实例
- Oracle存储过程实例
- oracle存储过程实例
- Oracle存储过程实例
- oracle存储过程实例
- oracle存储过程实例
- oracle存储过程实例
- oracle存储过程实例
- oracle存储过程实例
- Oracle存储过程实例
- oracle存储过程实例
- oracle-存储过程实例
- oracle存储过程实例
- Python备份数据库并定期删除n天前的备份数据(本地)
- data too long for column的解决方法
- php的where查询
- C# 中的char 和 byte
- mysql取别名 column not found
- Oracle存储过程实例
- 代码重构方法
- c语言数据类型
- 设计模式-工厂模式
- 光电耦合器电路应用符号
- Python安装scrapy库过程中出现 " Failed building wheel for Twisted"
- java script入门
- 【Java高级开发工程师】近一个月的面试总结
- LeetCode@111_Minimum_Depth_of_Binary_Tree