Note that there are some explanatory texts on larger screens.

plurals
  1. POgeneric DAO in java
    primarykey
    data
    text
    <p>I am trying to develop generic DAO in java. I have tried the following. Is this a good way to implement generic DAO? I don't want to use hibernate. I am trying to make it as generic as possible so that I don't have to repeat the same code over and over again.</p> <pre><code>public abstract class AbstractDAO&lt;T&gt; { 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; } } } </code></pre> <p>Now I have:</p> <pre><code>public class UserDAO extends AbstractDAO&lt;User&gt;{ public List&lt;User&gt; findbyid(int id){ Resultset rs =findbyid("USERS",id) // "USERS" is table name in DB List&lt;Users&gt; users = convertToList(rs); return users; } private List&lt;User&gt; convertToList(ResultSet rs) { List&lt;User&gt; userList= new ArrayList(); User user= new User();; try { while (rs.next()) { user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setFname(rs.getString("fname")); user.setLname(rs.getString("lname")); user.setUsertype(rs.getInt("usertype")); user.setPasswd(rs.getString("passwd")); userList.add(user); } } catch (SQLException ex) { Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex); } return userList; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload