2013-10-28 44 views
0

我在Oracle數據庫的功能(不是過程)。 這個功能看起來像:如何得到一個結果了PL/SQL函數的Netbeans中

CREATE OR REPLACE FUNCTION GETTOTAL(v_user_id IN NUMBER) 
RETURN NUMBER 
AS 
    v_result number := 0; 
BEGIN 
    SELECT SUM(DAY1+DAY2) INTO v_result FROM TABLE WHERE USER_ID = v_user_id; 
RETURN v_result; 
END; 

現在我在Java Netbeans的程序,我需要的是結果,用我的計劃內。

我曾嘗試以下:

callStatement = con.prepareCall("SELECT GETTOTAL(1) FROM DUAL;"); 
callStatement.execute(); 
resultaat = callStatement.getDouble(1); 
callStatement.close(); 

我也曾嘗試使用CALL。但似乎沒有任何工作。 另外我試過尋找網絡上的問題,但似乎只有程序解釋,而不是功能......所以我希望我可以在這裏找到一個awnser。

回答

1

檢查我的例子:

CREATE TABLE my_test_tab (
    user_id NUMBER, 
    day1 NUMBER, 
    day2 NUMBER 
); 

INSERT INTO my_test_tab VALUES (1, 5, 10); 
INSERT INTO my_test_tab VALUES (1, 1, 2); 

COMMIT; 

CREATE OR REPLACE FUNCTION GETTOTAL(v_user_id IN NUMBER) 
RETURN NUMBER 
AS 
    v_result number := 0; 
BEGIN 
    SELECT SUM(DAY1+DAY2) INTO v_result FROM my_test_tab WHERE USER_ID = v_user_id; 
RETURN v_result; 
END; 
/

在Java中,您創建一個CallableStatement,你必須registerOutParameter爲函數的返回值,檢查代碼:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.CallableStatement; 

public class Main2 { 
    public static void main(String[] args) throws Exception { 
    Connection conn = getOracleConnection(); 
    System.out.println("Got Connection."); 

    CallableStatement callStmt = null; 

    try { 
     callStmt = conn.prepareCall("{? = call gettotal(?)}"); 
     callStmt.setInt(2, 1); 
     callStmt.registerOutParameter(1, java.sql.Types.NUMERIC); 
     callStmt.execute(); 

     System.out.println(callStmt.getInt(1)); 
    } finally { 
     callStmt.close(); 
     conn.close(); 
    } 
    } 

    public static Connection getOracleConnection() throws Exception { 
    String driver = "oracle.jdbc.driver.OracleDriver"; 
    String url = "jdbc:oracle:thin:@HOST_ADDRESS:1521:orcl"; 
    String username = "USERNAME"; 
    String password = "PASSWORD"; 

    Class.forName(driver); // load Oracle driver 

    java.util.Properties info = new java.util.Properties(); 
    info.put ("user", "hr"); 
    info.put ("password", "oracle"); 
    Connection conn = DriverManager.getConnection(url, info); 

    return conn; 
    } 
} 
+0

謝謝!這正是我所需要的,我曾嘗試使用registerOutparamater,但在執行後我使用它。我的謝意很棒! – user2919688

0

您需要使用

String getDBUSERByUserIdSql = "{call GETTOTAL(?)}"; 
callableStatement = dbConnection.prepareCall(getDBUSERByUserIdSql); 
callableStatement.setInt(1, 10); 
+0

你可以在這裏得到一些幫助:http://stackoverflow.com/questions/8747937/preparedstatement-callablestatement-and-performance-considerations – RamHS

+0

謝謝你的快速awnser,但我怎麼得到返回結果?在你編寫的代碼中,它只被執行了嗎? – user2919688

0

試試這個:

String getDBUSERByUserIdSql = "{call GETTOTAL(?, ?)}"; 
callableStatement = dbConnection.prepareCall(getDBUSERByUserIdSql); 
callableStatement.setInt(1, 10); 
callableStatement.registerOutParameter(2, Types.INTEGER); 
callableStatement.execute(); 

int retVal = callableStatement.getInt(2); 
相關問題