正在創建一個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!
我創建了,但同樣的錯誤。 – 2014-09-29 05:42:56
out參數不會爲程序帶來任何價值,但它會返回一些值給java程序。這裏的date_info是一個out參數,用在condition子句中。那是問題請檢查一次。 – 2014-09-29 06:05:12
是的,我檢查並更改爲「IN date_info date」,即使這樣結果也是如此。 – 2014-09-29 06:26:14