我正在尝试在java中开发通用DAO.我尝试了以下内容.这是实现通用DAO的好方法吗?我不想使用hibernate.我试图使它尽可能通用,这样我就不必一遍又一遍地重复相同的代码.
public abstract class AbstractDAO<T> {
protected ResultSet findbyId(String tablename, Integer id){
ResultSet rs= null;
try {
// the following lines are not working
pStmt = cn.prepareStatement("SELECT * FROM "+ tablename+ "WHERE id = ?");
pStmt.setInt(1, id);
rs = pStmt.executeQuery();
} catch (SQLException ex) {
System.out.println("ERROR in findbyid " +ex.getMessage() +ex.getCause());
ex.printStackTrace();
}finally{
return rs;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我现在有:
public class UserDAO extends AbstractDAO<User>{
public List<User> findbyid(int id){
Resultset rs =findbyid("USERS",id) // "USERS" is table name in DB
List<Users> users …Run Code Online (Sandbox Code Playgroud)