I am trying to develop Generic DAO in Java. I have tried the following: Is this a good way to implement general DAO? I do not want to use hibernate, I'm trying to make it as normal as possible so that I do not have to recline the same code again.
Public abstract class AbstractDAO & lt; T & gt; {Safe ResultSet findbyId (string tablelame, integer ID) {ResultSet rs = null; Try {// the following lines pstmt = cn.prepareStatement ("SELECT * FROM" + tablename + "WHERE id =?") Are not working; PStmt.setInt (1, id); Rs = pStmt.executeQuery (); } Hold (SQLException pre) {System.out.println ("find in error" + ex.getMessage () + ex.getcause ()); Ex.printStackTrace (); } Finally {RS; }}} I now have:
Public class UserDAO added AbstractDAO & lt; User & gt; {Public listing & lt; User & gt; Findbyid (int id) {Resultset rs = findbyid ("USERS", id) // "USERS" DB list in table name & lt; Users & gt; User = convertist (rs); Return user; } Private listing & lt; Users & gt; Convertist (as a result of RS) {list & lt; Users & gt; UserList = new arrelist (); User user = new user () ;; Try {while (rs.next ()) {user.setId (rs.getInt ("id")); User.setUsername (rs.getString ("user name")); User.setFname (rs.getString ("fname")); User.setLname (rs.getString ("lname")); User.setUsertype (rs.getInt ("user type")); User.setPasswd (rs.getString ("password")); UserList.add (user); }} Hold (SQLException pre) {Logger.getLogger (UserDAO.class.getName ()). Log (Level.SEVERE, null, ex); } Return userList; }}
If you can live with the spring, then I suggest the following improvements Will:
- Spring exception handles.
- Use the Jdbc template instead of creating a statement.
Free to use the spring, I recommend the following:
- Do not send the table name as the parameter should be done in this initial step .
- Use a string on the id parameter, because it is more common.
- Consider retracting a common object instead of collection, since the collection should always be an object.
A better gamut with spring:
import java.util.Collection; Import org.springframework.jdbc.core.Jdbc template; Import org.springframework.jdbc.core.RowMapper; Public Abstract Class Sardo & lt; T & gt; {Protected Last Romper & lt; T & gt; RowMapper; Protected Last String FindByIdSql; Protected Last JDBC Template JDBC Template; Protected Abristado (Raumper on Rowmapper, String TableName, JDBC Template JDBC Template) {this.rowMapper = rowMapper; This.findByIdSql = "SELECT * FROM" + tableName + "WHERE id =?"; This.jdbcTemplate = jdbcTemplate; } Public Collection & lt; T & gt; FindById (last string id) {object] Parameters = {ID}; Return jdbcTemplate.query (findByIdSql, parameter, rowmapper); }} As you see, handling or hacking with no exceptions with the primitive SQL classes. This template closes the ResultSet for you, which I can not see in your code.
More User:
import java.sql.ResultSet; Import java.sql.SQLException; Import org.springframework.jdbc.core.Jdbc template; Import org.springframework.jdbc.core.RowMapper; Public class Userdesk abstraction & lt; User & gt; {Private final static string TABLE_NAME = "USERS"; Public Userdesign (JDBC Template JDBC Template) {Super (New UserRoom), Table_ NAME, JDBC Template); } Private Static Class UserRowMapper, applies RowMapper & lt; Users & gt; {Public user mapRow (ResultSet rs, int rowNum) throws SQLException {user user = new user (); User.setUserName (rs.getString ("user name")); User.setFirstName (rs.getString ("fname")); User.setLastName (rs.getString ("lname")); Return user; }} Updated:
When you match a line in the ID and the ID database, you should consider Returning a normal object instead of a collection.
public T findUniqueObjectById (last string id) {object [] parameters = {id}; Return jdbcTemplate.queryForObject (findByIdSql, parameter, rowmapper); } This makes your service code more readable, because you do not need to recover a user from a list, but only:
User user = userDao.findUniqueObjectById ("22");
Comments
Post a Comment