我们应该如何使用Hibernate处理DAO层中的SQLExceptions?
答案 0 :(得分:0)
创建class EmployeeDaoImpl.java
package com.mypro.module.emp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import com.mypro.module.emp.exception.EmployeeException;
import com.mypro.module.emp.model.Employee;
public class EmployeeDaoImpl extends JdbcDaoSupportImpl implements EmployeeDao {
@Override
public int save(Employee employee) throws EmployeeException {
// TODO Auto-generated method stub
String sql = "INSERT INTO EMPLOYEE VALUES(?,?,?)";
Connection conToUse = null;
PreparedStatement ps = null;
int status = 0;
try {
conToUse = getConnection();
ps = conToUse.prepareStatement(sql);
ps.setInt(1, employee.getEmpNo());
ps.setString(2, employee.getEmpName());
ps.setLong(3, employee.getEmpSal());
status = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new EmployeeException(
"%%% Exception occured in EmployeeDao save() %%% " + e);
} finally {
DbUtils.closeQuietly(ps);
}
return status;
}
}