example 1:
/*
* this sample shows how to call a pl/sql stored procedure using the sql92
* syntax. see also the other sample plsql.java.
*/
import java.sql.*;
import java.io.*;
class plsqlexample
{
public static void main (string args [])
throws sqlexception, ioexception
{
// load the driver
drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
string url = "jdbc:oracle:oci8:@";
try {
string url1 = system.getproperty("jdbc_url");
if (url1 != null)
url = url1;
} catch (exception e) {
// if there is any security exception, ignore it
// and use the default
}
// connect to the database
connection conn =
drivermanager.getconnection (url, "scott", "tiger");
// create a statement
statement stmt = conn.createstatement ();
// create the stored function
stmt.execute ("create or replace function raisesal (name char, raise number) return number is begin return raise + 100000; end;");
// close the statement
stmt.close();
// prepare to call the stored procedure raisesal.
// this sample uses the sql92 syntax
callablestatement cstmt = conn.preparecall ("{? = call raisesal (?, ?)}");
// declare that the first ? is a return value of type int
cstmt.registeroutparameter (1, types.integer);
// we want to raise leslies salary by 20,000
cstmt.setstring (2, "leslie"); // the name argument is the second ?
cstmt.setint (3, 20000); // the raise argument is the third ?
// do the raise
cstmt.execute ();
// get the new salary back
int new_salary = cstmt.getint (1);
system.out.println ("the new salary is: " + new_salary);
// close the statement
cstmt.close();
// close the connection
conn.close();
}
}
example 2:
/*
* created on 2004-10-12
*
* todo to change the template for this generated file go to
* window – preferences – java – code style – code templates
*/
/**
* @author jackey
*
* todo to change the template for this generated type comment go to
* window – preferences – java – code style – code templates
*/
/*
* this sample can be used to check the jdbc installation.
* just run it and provide the connect information. it will select
* "hello world" from the database.
*/
// you need to import the java.sql package to use jdbc
import java.sql.*;
// we import java.io to be able to read from the command line
import java.io.*;
import oracle.jdbc.oracletypes;
class jdbccheckup
{
public static void main(string args[])
throws sqlexception, ioexception
{
// load the oracle jdbc driver
drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
// prompt the user for connect information
system.out.println("please enter information to test connection to the database");
string user;
string password;
string database;
user = readentry("user: ");
int slash_index = user.indexof(/);
if (slash_index != -1)
{
password = user.substring(slash_index + 1);
user = user.substring(0, slash_index);
}
else
password = readentry("password: ");
database = readentry("database(a tnsname entry): ");
system.out.print("connecting to the database…");
system.out.flush();
system.out.println("connecting…");
connection conn = drivermanager.getconnection
("jdbc:oracle:oci8:@" + database, user, password);
system.out.println("connected.");
// create a statement
statement stmt = conn.createstatement();
// do the sql "hello world" thing
resultset rset = stmt.executequery("select hello world from dual");
while (rset.next())
system.out.println(rset.getstring(1));
// close the result set, the statement and connect
rset.close();
stmt.close();
system.out.println("your jdbc installation is correct.");
//
callablestatement call = conn.preparecall("{call emp_dept_data.open_cv(?,?)}");
// find out all the sales person
// call.registeroutparameter (1, oracletypes.cursor);
call.registeroutparameter (1 , oracletypes.cursor);
call.setint(2, 1);
call.execute ();
resultset rs = (resultset)call.getobject (1);
// dump the cursor
while (rs.next ())
system.out.println (rs.getstring (1)+"\t"+ rs.getstring(2)+"\t"+ rs.getstring(3));
rs.close();
call.close();
conn.close();
}
// utility function to read a line from standard input
static string readentry(string prompt)
{
try
{
stringbuffer buffer = new stringbuffer();
system.out.print(prompt);
system.out.flush();
int c = system.in.read();
while (c != \n && c != -1)
{
buffer.append((char)c);
c = system.in.read();
}
return buffer.tostring().trim();
}
catch(ioexception e)
{
return "";
}
}
}