当前位置:网站首页>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 .
边栏推荐
- Poj3414广泛搜索
- Hysbz 2243 staining (tree chain splitting)
- Advantages of robot framework
- 装饰器学习01
- Meituan dynamic thread pool practice ideas, open source
- About the writing method of SQL field "this includes" and "included in" strings
- Granularity of blocking of concurrency control
- 2.2.3 output of documents
- Comment développer un plug - in d'applet
- 1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
猜你喜欢
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
2.2.3 output of documents
Business learning of mall commodity module
Business learning of mall order module
Summary of concurrency control
Index optimization of performance tuning methodology
Defect detection - Halcon surface scratch detection
Meituan dynamic thread pool practice ideas, open source
Database recovery strategy
EBS Oracle 11g cloning steps (single node)
随机推荐
poj 3237 Tree(树链拆分)
Defect detection - Halcon surface scratch detection
Robot operation mechanism
Yolov5 training custom data set (pycharm ultra detailed version)
Performance monitoring of database tuning solutions
Codeforces 12D ball tree array simulation 3 sorting elements
PyGame practical project: write Snake games with 300 lines of code
Business learning of mall commodity module
ICMP 介绍
Poj3414 extensive search
EBS Oracle 11g cloning steps (single node)
Exercise 1 simple training of R language drawing
Sentinel production environment practice (I)
Oracle HugePages没有被使用导致服务器很卡的解决方法
Robot framework setting variables
Two stage locking protocol for concurrency control
怎么利用Tensorflow2进行猫狗分类识别
装饰器学习01
Regular expressions and re Libraries
Pointer parameter passing vs reference parameter passing vs value parameter passing