Oracle存储过程简单实例:
一、不带任何参数存储过程(输出系统日期)
create or replace procedure output_date is
begin
dbms_output.put_line(sysdate);
end output_date;
二、带参数in和out的存储过程
create or replace procedure get_username(v_id in number,v_username out varchar2)
as
begin
select username into v_username from tab_user where id = v_id; --变量赋值
exception
when no_data_found then
raise_application_error(-20001,'ID不存在!');
end get_username;
上文中通过实例的分析,为大家介绍了Oracle数据库存储的知识,将具体问题形象化,这样更利于大家去理解学习,希望上文中涉及到的内容对大家能够有所帮助。
//java调用一个存储过程
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Types;
- public class TestProcedureTwo {
- public TestProcedureTwo() {
- }
- public static void main(String[] args ){
- String driver = "oracle.jdbc.driver.OracleDriver";
- String strUrl = "jdbc:oracle:thin:@localhost:1521:myoracle";
- Statement stmt = null;
- ResultSet rs = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(strUrl, "system", "admin");
- CallableStatement proc = null;
- proc = conn.prepareCall("{ call dem_procedure(?,?) }");
- proc.setString(1, "kalision");
- proc.registerOutParameter(2, Types.VARCHAR);
- proc.execute();
- String testPrint = proc.getString(2);
- System.out.println("存储过程返回的值是:"+testPrint);
- }
- catch (SQLException ex2) {
- ex2.printStackTrace();
- }
- catch (Exception ex2) {
- ex2.printStackTrace();
- }
- finally{
- try {
- if(rs != null){
- rs.close();
- if(stmt!=null){
- stmt.close();
- }
- if(conn!=null){
- conn.close();
- }
- }
- }
- catch (SQLException ex1) {
- }
- }
- }
- }
SQL>variable v_i varchar2(2000);
SQL>execute A(:v_i,123,'ABC');
SQL>print :v_i;