码迷,mamicode.com
首页 > 数据库 > 详细

oracle pl/sql之在java中怎么调用oracle函数

时间:2015-10-22 19:00:37      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

建立函数:

create or replace function my_fun(in_no in number) return varchar2 is
out_name varchar2(64);
begin
select ename into out_name from emp where emp.empno=in_no;
return out_name;
end my_fun;

 

java程序演示:

 

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;


public class JavaCallMath {
public static void main(String[] args) {
  String driver="oracle.jdbc.driver.OracleDriver";
  String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  String user="scott";
  String password="ztoracle";
  try {
    Class.forName(driver);
    Connection c=DriverManager.getConnection(url, user, password);
    CallableStatement cs=c.prepareCall("{?=call my_fun(?)}");
    cs.setInt(2, 9527);
  cs.registerOutParameter(1, Types.VARCHAR);
  cs.execute();
  System.out.println(cs.getString(1));
} catch (ClassNotFoundException e) {
throw new RuntimeException();
} catch (SQLException e) {

}
}

}

 

oracle pl/sql之在java中怎么调用oracle函数

标签:

原文地址:http://www.cnblogs.com/ztyy04126/p/4901657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!