Home Database Mysql Tutorial Oracle查询所有序列 JAVA+Oracle Function全攻略

Oracle查询所有序列 JAVA+Oracle Function全攻略

Jun 07, 2016 pm 05:23 PM
oracle sequence

Oracle查询所有序列 JAVA+Oracle Function全攻略

--查看当前用户的所有序列

select SEQUENCE_OWNER,SEQUENCE_NAME from dba_sequences where sequence_owner='用户名'; 

注意事项:

    1.      必须以管理员身份登录;

    2.      sequence_owner必须为大写,不管你的用户名是否大写。只有大写才能识别。

以一张BOOK表的查询为例:

create table BOOK
(
  BOOKNUMBER CHAR(3) not null,
  BOOKNAME  VARCHAR2(50),
  BOOKPRICE  NUMBER(18,2)
)

要返回数据集,就需要使用Oracle里的reference cursor类型,不过Oracle里这个类型用的方式比较罗唆(搞不明白Oracle为什么要弄的这么复杂),首先要声明一个引用的cursor类型,这个通过创建一个包来实现:

CREATE OR REPLACE PACKAGE PKG_TEST
AS
    TYPE REFCURSOR IS REF CURSOR;
END PKG_TEST;

 
然后创建我们的function存储过程:
create or replace function query_book(key varchar) return PKG_TEST.REFCURSOR is
  Result PKG_TEST.REFCURSOR;
begin
  open Result for
      select * from book where key=book.booknumber;
     
  return(Result);
end query_book;

在这个存储过程中,我们传入了查询键值key,返回了一个数据集对象
这样我们就有了一个名为query_book的存储过程,它可以返回一个结果数据集。要调用这个存储过程,可以通过CallableStatement接口来调用,,这样需要写成类似'{?=call query_book(?)}'的查询语句,并且参数和返回值都必须用代码设置;在这里,一些简单的存储过程,也可以通过查询来调用。下面就是我使用的调用方法:

public class FuncTest {
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection  connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:LeeDB",
        "TEST", "test");
    Statement  stmt = connection.createStatement();
    ResultSet  dataset = stmt.executeQuery("SELECT query_book('001') FROM DUAL");
    if (dataset.next()) {
      ResultSet  ret = (ResultSet)dataset.getObject(1);
     
      while( ret.next() ) {
        System.out.print(ret.getString(1));
        System.out.print("\t");
        System.out.print(ret.getString(2));
        System.out.print("\t");
        System.out.println(ret.getDouble(3));
      }
    }
  }
}

注意这里返回的数据集中内嵌了function返回的数据集,需要进行特殊处理。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles