2014-09-29 95 views
0

正在創建一個java代碼來調用mysql db中的存儲過程。 但是當我編譯代碼時出現以下錯誤。我附上了代碼。是一個初學存儲過程尋求一些幫助。提前感謝。 存儲過程:通過Java調用存儲過程(MySql)時出錯

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_in_out_status_for_manager`(IN id int, OUT date_info date,out empname varchar(35), 
     out EmpID varchar(10), 
     out CardId Varchar(10), 
     out EntryTime Datetime, 
     out ExitTime datetime, 
     out WorkTime time) 
     BEGIN 
     SELECT Date,empname,EmpID,CardID,EntryTime,ExitTime,WorkTime from pax_attd 
     join employee_master on right(pax_attd.EmpID,3)=employee_master.employee_id 
     where ((employee_master.reporting_employee_id=id) and (date=date_info)); 

     END 

     Java Code: 

     package mysqltoxl; 
     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/PaxWeb"; 

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

      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_in_out_status_for_manager(?, ?, ?, ?, ?, ?, ?, ?)}"; 
        stmt = conn.prepareCall(sql); 

        //Bind IN parameter first, then bind OUT parameter 
        int EmpId = 401; 

        stmt.setInt(1,EmpId); 



        // Because second parameter is OUT so register it 

        stmt.registerOutParameter(2, java.sql.Types.DATE); 
        stmt.registerOutParameter(3, java.sql.Types.VARCHAR); 
        stmt.registerOutParameter(4, java.sql.Types.VARCHAR); 
        stmt.registerOutParameter(5, java.sql.Types.VARCHAR); 
        stmt.registerOutParameter(6, java.sql.Types.TIME); 
        stmt.registerOutParameter(7, java.sql.Types.TIME); 
        stmt.registerOutParameter(8, java.sql.Types.TIME); 

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


        Date Date = stmt.getDate(2); 
        String empname = stmt.getString(3); 
        String EmpID = stmt.getString(4); 
        String CardId = stmt.getString(4); 
        Date EntryTime= stmt.getDate(5); 
        Date ExitTime = stmt.getDate(6); 
        Time WorkTime = stmt.getTime(7); 
        System.out.println("Emp Name with ID:" + 
          EmpId + " is " + empname); 
        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 

我的錯誤是:

Connecting to database... 
Creating statement... 
Executing stored procedure... 
Emp Name with ID:401 is null 
Goodbye! 

回答

0

改變了參數名稱,並添加到子句中的select語句。 創建過程如下:

CREATE DEFINER = root @localhost PROCEDURE get_in_out_status_for_manager
IN ID INT,
OUT date_info日期,
out_empname VARCHAR(35),
out_EmpID VARCHAR( 10),
out out_CardId Varchar(10),
out out_EntryTime日期時間,
out_ExitTime日期時間,
out_WorkTime時間)
BEGIN
選擇日期,empname,的EmpID,CardId中,EntryTime,EXITTIME,工作時間
到date_info,out_empname,out_EmpID,out_CardID, out_EntryTime,out_ExitTime,out_WorkTime
從pax_attd 上右連接employee_master(pax_attd.EmpID,3)= employee_master.employee_id 其中((employee_master.reporting_employee_id = ID)和(日期= date_info));

END 
+0

我創建了,但同樣的錯誤。 – 2014-09-29 05:42:56

+0

out參數不會爲程序帶來任何價值,但它會返回一些值給java程序。這裏的date_info是一個out參數,用在condition子句中。那是問題請檢查一次。 – 2014-09-29 06:05:12

+0

是的,我檢查並更改爲「IN date_info date」,即使這樣結果也是如此。 – 2014-09-29 06:26:14

0

您是否忘記在存儲過程中進行操作?

CREATE DEFINER = root @localhost PROCEDURE get_in_out_status_for_manager(IN ID INT,IN date_info日期,出empname VARCHAR(35), 出的EmpID VARCHAR(10), 出CardId中VARCHAR(10), 出EntryTime日期時間, 出EXITTIME日期時間, 了工作時間的時間) BEGIN 選擇日期,empname,的EmpID,CardId中,EntryTime,EXITTIME,工作時間INTO DATE_INFO,EMPNAME,EMPID,CardId中,ENTRYTIME,EXITTIME,從pax_attd 工作時間加入employee_master上右(pax_attd.EmpID,3)= EMP loyee_master.employee_id where((employee_master.reporting_employee_id = id)and(date = date_info));

END 
+0

我試着按照你所說的,同樣的錯誤 – 2014-09-29 05:43:41

+0

date_info應該是IN,你需要在你的Java代碼中設置它。因爲從你選擇的查詢中你在where子句中使用date_info參數。 – 2014-09-29 05:56:18

+0

你已經把它作爲IN,但你要聲明的Java代碼? int EmpId = 401; stmt.setInt(1,EmpId); stmt.java.sql.Date.valueOf(2,「2013-09-04」); – 2014-09-29 07:26:51