当前位置:网站首页>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 .

边栏推荐
- Huawei fast game failed to call the login interface, and returned error code -1
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
- poj 3237 Tree(树链拆分)
- The American Championship is about to start. Are you ready?
- Deeply convinced plan X - network protocol basic DNS
- Some common processing problems of structural equation model Amos software
- Image editor for their AutoLayout environment
- EL与JSTL注意事项汇总
- database mirroring
猜你喜欢

Learning of mall permission module

华为云ModelArts文本分类–外卖评论

Scenario interview: ten questions and ten answers about distributed locks

Oracle checkpoint queue - Analysis of the principle of instance crash recovery

华为快游戏调用登录接口失败,返回错误码 -1

Dbeaver executes multiple insert into error processing at the same time

Daily question brushing record (XIV)

Sentinel production environment practice (I)

Emotional analysis of wechat chat records on Valentine's day based on Text Mining

Business learning of mall order module
随机推荐
Installation of VMware Workstation
Shell script, awk uses if, for process control
Overview of database recovery
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
NET中小型企业项目开发框架系列(一个)
Recovery technology with checkpoints
他们主动布局(autolayout)环境的图像编辑器
大约SQL现场“这包括”与“包括在”字符串的写法
QML reported an error expected token ";", expected a qualified name ID
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
Dbeaver executes multiple insert into error processing at the same time
Cross end solutions to improve development efficiency
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
Decorator learning 01
Sentinel production environment practice (I)
Analyse des risques liés aux liaisons de microservices
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Detailed explanation of memset() function usage
Robot operation mechanism
Drawing HSV color wheel with MATLAB