For get the connection of oracle database ,you should add the oracle driver to the classpath first.and when you finished ,should close all the resouces.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectDatabase {
public static void main(String args[]){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
PreparedStatement stmt = null;
Connection conn = null;
ResultSet rs = null;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,"jil","jil");
stmt = conn.prepareStatement("select * from class");
rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("CLASS_NUMBER"));
System.out.println(rs.getString("CLASS_ID"));
System.out.println(rs.getString("CLASS_REMARK"));
System.out.println(rs.getString("CLASS_NAME"));
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//release the resouces,the order is resultset,preparestatement,connection
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
f(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
In order to call the procedure, we should create the procedure first:
create or replace procedure TQ_PROC8(classInfo out myCursor.mycur) is
begin
open classinfo for select * from class;
end TQ_PROC8;
create or replace package myCursor is
-- Author : TANGLEI
-- Created : 2009-9-15 16:04:50
-- Purpose : for using cursor
-- Public type declarations
type mycur is ref cursor;
procedure TQ_PROC8(p_cur out mycur);
end myCursor;
create or replace package body myCursor is
procedure tq_proc8(p_cur out mycur) is
begin
open p_cur for select * from class;
end;
end myCursor;
then:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CallProcedure {
public static void main(String args[]){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
CallableStatement stmt = null;
Connection conn = null;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,"jil","jil");
stmt = conn.prepareCall("call JIL.TQ_PROC8(?)");
stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
stmt.execute();
ResultSet outRS =(ResultSet) stmt.getObject(1);
while(outRS.next()){
System.out.println(outRS.getString(1));
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//release the resouces
try {
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Both of these two examples have been tested :)
分享到:
相关推荐
ARM THUMB procedure call standard ...o Define more precisely the mapping of C, C++, and Java source language entities to the machine-level values passed as parameters across publicly visible interfaces.
contains all the source code in the book and provides a development and exploitation environment you can use to follow along with the book’s examples and experiment along the way.
In contrast, most software developers only ever get to know a handful of large programs well—usually programs they wrote themselves—and never study the great programs of history. As a result, they ...
The Art of Memory Forensics: Detecting Malware and Threats in Windows, Linux, and Mac Memory Memory forensics provides cutting edge technology to help investigate digital attacks Memory forensics is ...
The Sourcebook of Parallel Computing (The Morgan Kaufmann Series in Computer Architecture and Design) The Sourcebook of Parallel Computing (The Morgan Kaufmann Series in Computer Architecture and ...
The Oracle Call Interface is an application programming interface (API) that allows an application developer to use a third-generation language's native procedure or function calls to access the ...
In the early days of computer science, the interactions of hardware, software, compilers, and operating system were simple enough to allow students to see an overall picture of how computers worked....
based BSD operating system, Kirk McKusick and George Neville-Neil deliver here the most comprehensive, up-to-date, and authoritative technical information on the internal structure of open source ...
public static OracleConnection GetConnection() { OracleConnection conn = new OracleConnection(connectionString); try { conn.Open(); return conn; } catch (Exception ex) { Console.WriteLine(...
在这本书中,埃里克·冯·希佩尔(Eric Von Hippel)质疑基本假设:新产品创新通常是由产品制造商开发的,并证明了创新发生在不同行业的不同地方。
It provides a complete, systematic analysis of the operating system source code, as well as a direct and complete understanding of the real operating system run-time structure. The author includes ...
The Architecture of Open Source Applications epub版本
It provides valuable guidance on exactly what the best coaches are now doing to get the most out of leaders, for now and into the future. Revealing core philosophies, critical capabilities, and the ...
A Technical Overview of VP9--the Latest Open-Source Video Codec Google has recently finalized a next generation open-source video codec called VP9, as part of the libvpx repository of the WebM project...
Now in its third edition, this best-selling book continues to bring you some of the best thinking on how to apply Oracle Database to produce scalable applications that perform well and deliver correct...
Troubleshooting with Wireshark: Locate the Source of Performance Problem ) By Laura Chappell Foreword by Gerald Combs Edit by Jim Aragon This book focuses on the tips and techniques used to identify ...
The Source Code Of Linux 0.01
This book contains a compendium of 25 papers published since the 1970s dealing with pi and associated topics of mathematics and computer science. The collection begins with a Foreword by Bruce Berndt....
The Performance of Open Source Applications, mobi格式