Posted on 2007-07-17 22:00 
kooyee 阅读(766) 
评论(0)  编辑  收藏  所属分类: 
Java  、
Database数据库技术 
			 
			
		 
		 import java.sql.*;
import java.sql.*;


 public class StProcExample
public class StProcExample  {
{
 public static void main(String[] args)
 public static void main(String[] args)

 throws SQLException
 throws SQLException  {
{
 int ret_code;
int ret_code;
 Connection conn = null;
Connection conn = null;

 try
 try  {
{
 //Load and register Oracle driver
  //Load and register Oracle driver
 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
 //Establish a connection
 //Establish a connection

 conn = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
 conn = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
 Oracle", "oratest", "oratest");
 Oracle", "oratest", "oratest");
 int i_deptno = 10;
 int i_deptno = 10;
 CallableStatement pstmt = conn.prepareCall("{call p_highest_
 CallableStatement pstmt = conn.prepareCall("{call p_highest_
 paid_emp(?,?,?,?)}");
 paid_emp(?,?,?,?)}");
 pstmt.setInt(1, i_deptno);
 pstmt.setInt(1, i_deptno);
 pstmt.registerOutParameter(2, Types.INTEGER);
 pstmt.registerOutParameter(2, Types.INTEGER);
 pstmt.registerOutParameter(3, Types.VARCHAR);
 pstmt.registerOutParameter(3, Types.VARCHAR);
 pstmt.registerOutParameter(4, Types.FLOAT);
 pstmt.registerOutParameter(4, Types.FLOAT);
 pstmt.executeUpdate();
 pstmt.executeUpdate();

 int o_empno = pstmt.getInt(2);
 int o_empno = pstmt.getInt(2);
 String o_ename = pstmt.getString(3);
 String o_ename = pstmt.getString(3);
 float o_sal = pstmt.getFloat(4);
 float o_sal = pstmt.getFloat(4);
 System.out.print("The highest paid employee in dept "
 System.out.print("The highest paid employee in dept "
 +i_deptno+" is: "+o_empno+" "+o_ename+" "+o_sal);
 +i_deptno+" is: "+o_empno+" "+o_ename+" "+o_sal);
 pstmt.close();
 pstmt.close();
 conn.close();
 conn.close();

 } catch (SQLException e)
  } catch (SQLException e)  {ret_code = e.getErrorCode();
{ret_code = e.getErrorCode();
 System.err.println(ret_code + e.getMessage()); conn.close();}
   System.err.println(ret_code + e.getMessage()); conn.close();}
 }
 }
 }
}
在database中调用function
 Select ValidWorkingDay(param1,prasm2) as IsWorking from dual
Select ValidWorkingDay(param1,prasm2) as IsWorking from dual 

 Select
Select  {t '14:20:50'} as ThisDate from dual
{t '14:20:50'} as ThisDate from dual
 Select UCA_VALIDATE('123456','WW','1') as Answer from dual
Select UCA_VALIDATE('123456','WW','1') as Answer from dual
 或者
或者

 SELECT ename FROM emp WHERE hiredate =
SELECT ename FROM emp WHERE hiredate =  {t '12:00:00'}"
{t '12:00:00'}"

Update语句
 For example, instead of using the fn keyword in embedded SQL92 syntax:
For example, instead of using the fn keyword in embedded SQL92 syntax:

 Statement stmt = conn.createStatement ();
Statement stmt = conn.createStatement ();
 stmt.executeUpdate("UPDATE emp SET ename = {fn CONCAT('My', 'Name')}");
stmt.executeUpdate("UPDATE emp SET ename = {fn CONCAT('My', 'Name')}");


 Use Oracle SQL syntax:
Use Oracle SQL syntax:

 stmt.executeUpdate("UPDATE emp SET ename = CONCAT('My', 'Name')");
stmt.executeUpdate("UPDATE emp SET ename = CONCAT('My', 'Name')");

 
Function Call Syntax
Oracle's JDBC drivers support the following procedure and function call syntax:
Procedure calls (without a return value):
{ call procedure_name (argument1, argument2,...) }
Function calls (with a return value):
{ ? = call procedure_name (argument1, argument2,...) }
当用于call Function时,在等号= 前的?是statment的第一个parameter. 是out出来的值