当前位置:网站首页>Stored procedures and stored functions
Stored procedures and stored functions
2022-07-05 22:02:00 【The sea of waves】
stored procedure And store function
Create stored procedure
create or replace procedure sayhelloword
as
-- The explanatory part
begin
dbms_out_put_line("Hello world");
end;
/
Calling stored procedure
- exec sayhelloword();
- begin sayhelloword(); sayhelloword(); end;
Create a storage function
create or replace function queryempincome(eno in number)
return number
as
-- Define variables to save employees' salaries and bonuses
psal emp.sal%type;
pcome emp.comm%type;
begin
-- Get the monthly salary and bonus of the employee
select sal,comm into psal,pcomm from emp where empno=eno;
-- Return the annual income directly
return psal*12 + nvl(pcomm,0);
end;
java How to access stored procedures and stored functions in programs
// Database connection (JDBC)
package com.claa.javabasic.Connect;
import java.sql.*;
/** * @Author: claa * @Date: 2021/06/27 09:43 * @Description: Oracle Jdbc The connection of */
public class JDBCUtilsO {
private static String driver = "oracle.jdbc.OracleDriver";
private static String url="jdbc:oracle:thin:@192.168.56.101:1521:orcl";
private static String user ="scott";
private static String password ="tiger";
// Register database driver
static {
try{
Class.forName(driver);
}catch (ClassNotFoundException e){
throw new ExceptionInInitializerError(e);
}
}
// Get the driver of the database
public static Connection getConnection(){
try{
return DriverManager.getConnection(url,user,password)
}catch (SQLException e){
e.printStackTrace();
}
return null;
}
// Release the resources of the database
public static void Release(Connection conn, Statement st, ResultSet rs) {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
drs = null;
}
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
st = null;
}
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
// Calls stored procedures and functions
package com.claa.javabasic.Connect;
import oracle.jdbc.OracleTypes;
import org.junit.Test;
import java.sql.CallableStatement;
import java.sql.Connection;
/** * @Author: claa * @Date: 2021/06/27 10:39 * @Description: */
public class TestProcedure {
@Test
public void testProcedure(){
// stored procedure
String sql = "{call queryempincome(?,?,?,?)} ";
// Storage function
//String sql = "{?=call queryempincome(?,?,?,?)} ";
Connection conn = null;
CallableStatement call = null;
try{
// Get a connection
conn = JDBCUtilsO.getConnection();
// Created by connection Statement
call = conn.prepareCall(sql);
// about in Parameters
call.setInt(1,7839);
// about Out Parameters
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3,OracleTypes.NUMBER);
call.registerOutParameter(4,OracleTypes.VARCHAR);
// Execution call
call.execute();
// Take out the results
String name = call.getString(2);
System.out.println(name);
}catch(Exception e){
e.printStackTrace();
}finally {
JDBCUtilsO.Release(conn,call,null);
}
}
}
There is something wrong , Welcome to the discussion .
Last , Welcome to pay attention to my wechat , What do you like , Collection , Forwarding is my greatest encouragement .
边栏推荐
- Drawing HSV color wheel with MATLAB
- Scenario interview: ten questions and ten answers about distributed locks
- Four components of logger
- Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
- Blocking of concurrency control
- Form artifact
- Index optimization of performance tuning methodology
- 数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
- Robot operation mechanism
- 2.2.5 basic sentences of R language drawing
猜你喜欢
MMAP
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Recovery technology with checkpoints
Some common processing problems of structural equation model Amos software
Meituan dynamic thread pool practice ideas, open source
总结出现2xx、3xx、4xx、5xx状态码的原因
Livelocks and deadlocks of concurrency control
2.2 basic grammar of R language
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Implementation technology of recovery
随机推荐
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
Blocking of concurrency control
Huawei cloud modelarts text classification - takeout comments
ICMP 介绍
Net small and medium-sized enterprise project development framework series (one)
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
大约SQL现场“这包括”与“包括在”字符串的写法
Codeforces 12D Ball 树形阵列模拟3排序元素
Talking about MySQL index
华为云ModelArts文本分类–外卖评论
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Oracle检查点队列–实例崩溃恢复原理剖析
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Advantages of robot framework
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Shell script, awk condition judgment and logic comparison &||
Codeforces 12D ball tree array simulation 3 sorting elements
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
Dbeaver executes multiple insert into error processing at the same time