通过Java调用存储过程(MySql)时出错

时间:2014-09-29 04:47:15

标签: java mysql stored-procedures jdbc

创建一个java代码来从mysql Db调用存储过程。 但是当我编译代码时,我得到以下错误。我附上了代码。我是存储过程的初学者寻求帮助。谢谢提前。         存储过程:

    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!

2 个答案:

答案 0 :(得分:0)

更改out参数名称并在select语句中添加into子句。 创建程序如下:

创建DEFINER = root @ localhost程序get_in_out_status_for_manager
    IN id int,
    OUT date_info日期,
    out out_empname varchar(35),
        out out_EmpID varchar(10),
        out out_CardId Varchar(10),
        out out_EntryTime 日期时间,
        out out_ExitTime datetime,
        out out_WorkTime 时间)
        BEGIN
        SELECT Date,empname,EmpID,CardID,EntryTime,ExitTime,WorkTime
    进入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         where((employee_master.reporting_employee_id = id)和(date = date_info));

    END

答案 1 :(得分:0)

您是否忘记了存储过程?

CREATE DEFINER = root @ localhost程序get_in_out_status_for_manager(IN id int, IN date_info date,out empname varchar(35),         出EmpID varchar(10),         出CardId Varchar(10),         输入时间日期时间,         退出ExitTime日期时间,         出工作时间)         开始         选择日期,empname,EmpID,CardID,EntryTime,ExitTime,WorkTime INTO 来自pax_attd的DATE_INFO,EMPNAME,EMPID,CARDID,ENTRYTIME,EXITTIME,WORKTIME         在右边加入employee_master(pax_attd.EmpID,3)= employee_master.employee_id         where((employee_master.reporting_employee_id = id)和(date = date_info));

    END