java 访问oracle存储过程

如何在JAVA中调用oracle的存储过程呢,下面是笔者给出的一个例子,大家有兴趣的话可以参考以下,如果程序中有什么不足的地方,恳请大家能够指点,谢谢:

/**
 *Copyright 2008,  CSSWEB  all rights reserved.
 *
@author hujun
 *@date Mar 13, 2008
 *@file DataHandler.java 
 *
@version 1.1
 *
*/


package  css.web.orc.jdbc;

import  java.sql.CallableStatement;
import  java.sql.Connection;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.sql.Statement;

import  oracle.jdbc.OracleTypes;
import  oracle.jdbc.pool.OracleDataSource;

public   class  DataHandler  {
    
private String jdbcUrl = "jdbc:oracle:thin:@localhost";
    
private String userid = "hr";
    
private String password = "123456";
    
private Connection conn;
    
    
private Statement stmt;
    
private ResultSet rset;
    
private String query;
    
private String sqlStr;
    
    
public DataHandler() {
    }

    
    
/**
     * get connection to oracle database
     * 
@throws SQLException
     
*/

    
public void getDBConnection() throws SQLException {
        OracleDataSource ds 
= new OracleDataSource();
        ds.setURL(jdbcUrl);
        conn 
= ds.getConnection(userid, password);
    }

    
    
/**
     * select
     * 
@return rset
     * 
@throws SQLException
     
*/

    
public ResultSet getAllEmployees() throws SQLException{
        getDBConnection();
        stmt 
= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_READ_ONLY);
        query 
= "SELECT * FROM Employees ORDER BY employee_id";
        System.out.println(
" Executing query: " + query);
        rset 
= stmt.executeQuery(query);
        
return rset;
    }

    
    
/**
     * select
     * 
@param name
     * 
@return rset
     * 
@throws SQLException
     
*/

    
public ResultSet getEmployeesByName(String name) throws SQLException {
        name 
= name.toUpperCase();
        getDBConnection();
        stmt 
= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
                ResultSet.CONCUR_READ_ONLY);
        query 
= "SELECT * FROM Employees WHERE UPPER(first_name) LIKE '%" +name+ "%'"+
        
" OR UPPER(last_name) LIKE '%" +name+ "%' ORDER BY employee_id";
        System.out.println(
" Executing query: " + query);
        rset 
= stmt.executeQuery(query);
        
return rset;
    }

    
    
/**
     * ask procedure
     * 
@return
     * 
@throws SQLException
     
*/

    
public ResultSet getJobs() throws SQLException {
        
try {
        getDBConnection();
        String jobquery 
= "begin ? := get_jobs; end;";
        CallableStatement callStmt 
= conn.prepareCall(jobquery);
        callStmt.registerOutParameter(
1, OracleTypes.CURSOR);
        callStmt.execute();
        rset 
= (ResultSet)callStmt.getObject(1);
        }
 catch ( SQLException ex ) {
            ex.printStackTrace();
        }

        
return rset;
    }

    
    
/**
     * insert
     * 
@param first_name
     * 
@param last_name
     * 
@param email
     * 
@param phone_number
     * 
@param job_id
     * 
@param salary
     * 
@return boolean
     * 
@throws SQLException
     
*/

    
public String addEmployeeSP(String first_name, String last_name,
            String email, String phone_number, String job_id,
            
int salary) throws SQLException {
        
try
        
{
            getDBConnection();
            sqlStr 
= "begin insert_employee(?,?,?,?,?,?); end;";
            CallableStatement callstmt 
= conn.prepareCall(sqlStr);
            callstmt.setString(
1, first_name);
            callstmt.setString(
2, last_name);
            callstmt.setString(
3, email);
            callstmt.setString(
4, phone_number);
            callstmt.setString(
5, job_id);
            callstmt.setInt(
6, salary);
            System.out.println(
" Inserting with stored procedure: " + sqlStr);
            callstmt.execute();
            
return "success";
        }

        
catch ( SQLException ex ) {
            ex.printStackTrace();
            System.out.println(
"Possible source of error: Make sure you have created the stored procedure");
            
return "failure";
        }
        
    }

}

下面是程序中使用的存储过程:

 

PROCEDURE  "INSERT_EMPLOYEE" ( p_first_name employees.first_name % type,
p_last_name employees.last_name
% type,
p_email employees.email
% type,
p_phone_number employees.phone_number
% type,
p_job_id employees.job_id
% type,
p_salary employees.salary
% type
)
AS
BEGIN
INSERT   INTO  Employees  VALUES  (EMPLOYEES_SEQ.nextval, p_first_name ,
p_last_name , p_email , p_phone_number, SYSDATE, p_job_id,
p_salary,.
30 , 100 , 80 );
END  insert_employee;

你可能感兴趣的:(java,oracle,存储,query,insert,import)