我使用的是Spring 3.0.2,我有一个叫做MovieDAO的类,它使用JDBC来处理数据库。我已经设置了@Repository注解,我想为SQLException转换为春天的DataAccessException我下面举个例子:如何在Spring中使用带有@Repository的SQLErrorCodeSQLExceptionTranslator和DAO类?
@Repository
public class JDBCCommentDAO implements CommentDAO {
static JDBCCommentDAO instance;
ConnectionManager connectionManager;
private JDBCCommentDAO() {
connectionManager = new ConnectionManager("org.postgresql.Driver", "postgres", "postgres");
}
static public synchronized JDBCCommentDAO getInstance() {
if (instance == null)
instance = new JDBCCommentDAO();
return instance;
}
@Override
public Collection<Comment> getComments(User user) throws DAOException {
Collection<Comment> comments = new ArrayList<Comment>();
try {
String query = "SELECT * FROM Comments WHERE Comments.userId = ?";
Connection conn = connectionManager.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
stmt = conn.prepareStatement(query);
stmt.setInt(1, user.getId());
ResultSet result = stmt.executeQuery();
while (result.next()) {
Movie movie = JDBCMovieDAO.getInstance().getLightMovie(result.getInt("movie"));
comments.add(new Comment(result.getString("text"), result.getInt("score"), user, result.getDate("date"), movie));
}
connectionManager.closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
//CONVERT TO DATAACCESSEXCEPTION
}
return comments;
}
}
我不知道如何让翻译,我不希望任何延伸Spring类,因为这就是为什么我使用@Repository注解