简体   繁体   中英

Error in calling Stored Procedure(MySql) through Java

Am creating a java code to call a stored procedure from mysql Db. But am getting this following error when i compile the code. I have attached the code. Am a beginner to stored procedures looking for some help.Thanks in advance. Stored Procedure:

    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

My Error is :

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

change the out parameter names and add into clause in select statement. create procedure as follows:

CREATE DEFINER= root @ localhost PROCEDURE get_in_out_status_for_manager (
IN id int,
OUT date_info date,
out out_empname varchar(35),
out out_EmpID varchar(10),
out out_CardId Varchar(10),
out out_EntryTime Datetime,
out out_ExitTime datetime,
out out_WorkTime time)
BEGIN
SELECT Date,empname,EmpID,CardID,EntryTime,ExitTime,WorkTime
into date_info,out_empname,out_EmpID,out_CardID,out_EntryTime,out_ExitTime,out_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

Did you forget to put into in the stored procedure?

CREATE DEFINER= root @ localhost PROCEDURE get_in_out_status_for_manager (IN id int, IN 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 INTO DATE_INFO, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM