欢迎光临
我们一直在努力

Java 中调用oracle 的过程-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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 "";

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java 中调用oracle 的过程-JSP教程,Java技巧及代码
分享到: 更多 (0)