Oracle 的jdbc方法
2018-06-18 00:20:08来源:未知 阅读 ()
package com.swift.jdbc_oracle; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.After; import org.junit.Before; import org.junit.Test; import oracle.jdbc.OracleTypes; import oracle.jdbc.driver.OracleCallableStatement; public class Oracle_jdbc { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@192.168.189.101:1521:orcl"; String username = "scott"; String password = "tiger"; Connection conn =null; PreparedStatement statement = null; ResultSet rs = null; CallableStatement call = null; OracleCallableStatement oraclecall = null; @Before public void init() throws ClassNotFoundException, SQLException { Class.forName(driver); conn = DriverManager.getConnection(url,username,password); } //普通查询 public void test() throws ClassNotFoundException, SQLException { String sql="select * from emp"; statement = conn.prepareStatement(sql); rs = statement.executeQuery(); while(rs.next()) { System.out.println(rs.getInt("empno")+"~"+rs.getString("ename")+"~"+rs.getString("sal")+"~" +rs.getString("comm")+"~"+rs.getString("deptno")); } System.out.println("Test~~~~~~~~~"); } //访问单返回值存储过程create or replace procedure getYearSal(eno in number,yearsal out number) //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} //sql语法格式 {call <procedure-name>[(<arg1>,<arg2>, ...)]} public void procedure() throws SQLException { String sql="{call getYearSal(?,?)}"; call = conn.prepareCall(sql); //输入的和之前preparestatement一样 call.setInt(1, 7788); //输出的要注册参数是什么类型 call.registerOutParameter(2,OracleTypes.NUMBER); call.execute(); //要先查询存储过程再输出结果 double yearsal = call.getDouble(2); System.out.println(yearsal); } //访问游标返回值存储过程create or replace procedure getEmps(dno in number,emps out sys_refcursor) public void procedure2() throws SQLException { String sql="{call getEmps(?,?)}"; call = conn.prepareCall(sql); //输入的和之前preparestatement一样 call.setInt(1, 20); //输出的要注册参数是什么类型 call.registerOutParameter(2,OracleTypes.CURSOR); call.execute(); //要先查询存储过程再输出结果 Object o = call.getObject(2); rs=(ResultSet)o; while(rs.next()) { System.out.println(rs.getInt("empno")+"~"+rs.getString("ename")+"~"+rs.getString("sal")+"~" +rs.getString("comm")+"~"+rs.getString("deptno")); } } //访问游标返回值存储过程(OracleCallableStatement)create or replace procedure getEmps(dno in number,emps out sys_refcursor) public void procedure3() throws SQLException { String sql="{call getEmps(?,?)}"; oraclecall = (OracleCallableStatement) conn.prepareCall(sql); //输入的和之前preparestatement一样 oraclecall.setInt(1, 20); //输出的要注册参数是什么类型 oraclecall.registerOutParameter(2,OracleTypes.CURSOR); oraclecall.execute(); //要先查询存储过程再输出结果 rs = oraclecall.getCursor(2); while(rs.next()) { System.out.println(rs.getInt("empno")+"~"+rs.getString("ename")+"~"+rs.getString("sal")+"~" +rs.getString("comm")+"~"+rs.getString("deptno")); } } //访问单返回值存储函数create or replace function x(eno in number) return number //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} public void function() throws SQLException { String sql="{?= call x(?)}"; call = conn.prepareCall(sql); //输入的和之前preparestatement一样 call.setInt(2, 7788); //输出的要注册参数是什么类型 call.registerOutParameter(1,OracleTypes.NUMBER); call.execute(); //要先查询存储过程再输出结果 double yearsal = call.getDouble(1); System.out.println(yearsal); } //访问单返回值存储函数create or replace function x(eno in number) return number //不用这个格式{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}用存储函数select方法 @Test public void function2() throws SQLException{ String sql="select x(7788) yearsal from dual";//不能多写一个; 写;会报无效字符异常 statement=conn.prepareStatement(sql); rs= statement.executeQuery(); while(rs.next()) { System.out.println(rs.getInt("yearsal")); } } @After public void close() throws SQLException { if(rs!=null) { rs.close(); } if(statement!=null) { statement.close(); } if(conn!=null) { conn.close(); } } }
需要先把Oracle包倒到项目中ojdbc14-10.2.0.1.0.jar
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 类的继承,方法重新中修饰符如何定义 2020-06-10
- JSP+Structs+JDBC+mysql实现的诚欣电子商城 2020-06-08
- Spring11_JdbcTemplate 2020-06-07
- java里面main方法中的String[]args 2020-06-07
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash