Oracle编程:idea调用Oracle数据库环境搭建、java调用存储函数及存储过程
程序员文章站
2022-04-22 09:42:20
Java调用存储过程环境搭建:导入依赖jar包 pom.xml com.oracle ojdbc14 10.2.0.4.0 ...
Java调用存储过程
环境搭建:导入依赖jar包 pom.xml(导jar包可能会遇到问题:因为涉及到收费问题,这时候可以手动导入,不会的可以留言,我私信回你。)
注:在导入ojdbc14的时候,遇到OracleTypes属性爆红不能使用,当我把runtime属性删掉就会好了。
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
测试环境:编写Java类查询scott中数据:
@Test
public void javaCallOracle() throws Exception{
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到Connection连接
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:数据库名字", "用户名", "密码");
//得到预编译的Statement对象
PreparedStatement pstm=connection.prepareStatement("select * from emp where empno=?");
//给参数赋值
pstm.setObject(1,7782);
//执行数据库查询操作
ResultSet rs=pstm.executeQuery();
//输出结果
while (rs.next()){
System.out.println(rs.getString("ename"));
}
//释放资源
rs.close();
pstm.close();
connection.close();
}
查询结果:成功获取Oracle数据库表中数据
注:忘记数据库名字可以在Oracle中输入命令查看
- 查看oracle所在服务器的ip:
select utl_inaddr.get_host_address from dual;
- 查看登陆oracle机器的IP:
select sys_context('userenv','ip_address') from dual;
- 查看oracle数据库的SID:
select instance_name from v$instance;
Java调用存储过程
存储过程代码:
create or replace
procedure p_yearsal(eno emp.empno%type,yealsal out number)
is
s number(10);
c emp.comm%type;
begin
select sal*12,nvl(comm,0) into s,c from emp where empno=eno;
yealsal := s+c;
end;
OracleDemo:
/**
* java调用存储过程
* 调用存储过程使用(Procedure):{call <procedure-name>[(<arg1>,<arg2>)]}
* @throws Exception
*/
@Test
public void javaCallProcedure() throws Exception{
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到Connection连接
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myowner", "scott", "Fz2019695269");
//得到预编译的Statement对象
CallableStatement pstm=connection.prepareCall("{call p_yearsal(?,?)}");
//给参数赋值
pstm.setObject(1,7782);
pstm.registerOutParameter(2,OracleTypes.NUMBER);
//执行数据库查询操作
pstm.execute();
//输出结果[第二个参数]
System.out.println(pstm.getObject(2));
//释放资源
pstm.close();
connection.close();
}
运行结果:成功调用。
Java调用存储函数
存储函数代码:
create or replace
function f_yearsal(eno emp.empno%type) return number
is
s number(10);
begin
select sal*12+nvl(comm,0) into s from emp where empno =eno;
return s;
end;
OracleDemo:
/**
* Java调用存储函数
* 调用存储函数使用(Function):{?=call <procedure-name>[(<arg1>,<arg2>)]}
* @throws Exception
*/
@Test
public void javaCallFunction() throws Exception{
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到Connection连接
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myowner", "scott", "Fz2019695269");
//得到预编译的Statement对象
CallableStatement pstm=connection.prepareCall("{?=call p_yearsal(?)}");
//给参数赋值
pstm.registerOutParameter(1,OracleTypes.NUMBER);
pstm.setObject(2,7782);
//执行数据库查询操作
pstm.execute();
//输出结果[第一个参数]
System.out.println(pstm.getObject(1));
//释放资源
pstm.close();
connection.close();
}
运行结果:成功调用。
本文地址:https://blog.csdn.net/ss977/article/details/109466144