2013-03-11 87 views
2

我在從java中的mysql存儲過程中檢索OUT參數時出現問題。從java中的mysql存儲過程中獲取參數

CALL proc_after_topic_add('newtest',@result); 
SELECT @result; 

此查詢給了我想要的輸出參數,但如何將我找回它在java.I使用CallableStatement的嘗試,但我得到

java.sql.SQLException: Callable statments not supported. 

error.Please人幫助我。 我曾嘗試以下

String sql = "CALL proc_after_topic_add(?,?);"; 
      CallableStatement cstmt = conn.prepareCall(sql); 
      cstmt.setString(1, topicname); 
      cstmt.registerOutParameter(2, java.sql.Types.INTEGER); 
      ResultSet rs = cstmt.executeQuery(); 
      if (rs.next()) { 
       if (rs.getInt(1) == 1) { 
        res = 0; 
       } else { 
        res = -1; 
       } 
      } 

我還沒有發佈的存儲過程的代碼,因爲沒有什麼不妥的地方。

PS:I a using mysql 5.5.21 and yes i should probably mention i am using mysql connector 3.0.15 

好吧,這是任何人都solved.For遇到誰了同樣的問題,只要下載最新版本的MySQL連接器。

回答

2

錯誤在這行

cstmt.registerOutParameter(2, java.sql.Types.INTEGER); 

改變這樣

String sql = "CALL proc_after_topic_add(?,?);"; 
cstmt.registerOutParameter(2, java.sql.Types.INTEGER); 
+0

的發生,但我仍然得到調用語句不支持error.Is這問題,MySQL連接或MySQL version.i上午uing MySQL的21年5月5日 – ntstha 2013-03-11 10:54:18

+0

Canu酒店調試這段代碼,並檢查是否錯誤拋出Sql過程,我不認爲不會有任何與版本相關的問題。但請檢查一下,即使你可以訪問Callable語句,然後從sql過程中發出問題。你可以請張貼Sql程序 – 2013-03-11 10:59:00

0
Create a schema test and create a table employee in schema with 2 columns. 
id and employeename and insert some data. 

Use this Stored Procedure. 
DELIMITER $$ 
DROP PROCEDURE IF EXISTS `test`.`get_count_name1` $$ 
CREATE PROCEDURE `test`.`get_count_name1`(IN the_name VARCHAR(64),OUT  
the_count INT) 

BEGIN 
SELECT COUNT(*) INTO the_count from employee where employeename=the_name; 
END$$ 
DELIMITER ; 

使用這個例子。 用戶名和密碼是我的根和根。根據您的要求更改
。在這裏,我就指望employeename =「迪派」

import java.sql.*; 
public class JDBCExample { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost/test"; 

    // Database credentials 
    static final String USER = "root"; 
    static final String PASS = "root"; 

    public static void main(String[] args) { 
    Connection conn = null; 
    CallableStatement stmt = null; 
    try{ 
     //STEP 2: Register JDBC driver 
     Class.forName("com.mysql.jdbc.Driver"); 

     //STEP 3: Open a connection 
     System.out.println("Connecting to database..."); 
     conn = DriverManager.getConnection(DB_URL,USER,PASS); 

     //STEP 4: Execute a query 
     System.out.println("Creating statement..."); 
     String sql = "{call get_count_name1 (?, ?)}"; 
     stmt = conn.prepareCall(sql); 

     //Bind IN parameter first, then bind OUT parameter 
     String name = "Deepak"; 
     stmt.setString(1, name); // This would set ID as 102 
     // Because second parameter is OUT so register it 
     stmt.registerOutParameter(2, java.sql.Types.INTEGER); 

     //Use execute method to run stored procedure. 
     System.out.println("Executing stored procedure..."); 
     stmt.execute(); 

    int count=stmt.getInt(2); 
    System.out.println(count); 
     stmt.close(); 
     conn.close(); 
    }catch(SQLException se){ 
     //Handle errors for JDBC 
     se.printStackTrace(); 
    }catch(Exception e){ 
     //Handle errors for Class.forName 
     e.printStackTrace(); 
    }finally{ 
     //finally block used to close resources 
     try{ 
     if(stmt!=null) 
      stmt.close(); 
     }catch(SQLException se2){ 
     }// nothing we can do 
     try{ 
     if(conn!=null) 
      conn.close(); 
     }catch(SQLException se){ 
     se.printStackTrace(); 
     }//end finally try 
    }//end try 
    System.out.println("Goodbye!"); 
}//end main 
}//end JDBCExample