While developing a Java application, developer always write some similar code over and over again.
For that, eclipse provides inbuilt templates for writing for loops, iterator loops, main method, try/catch blocks, and many more which saves hail lot of time.
Not only that but eclipse also provides a facility to write our own customized templates.
So here I am adding my custom templates.. I will keep on adding here as whenever I find repetitive code and also get chance to write new ones.
Starting with DAO Insert method:
public int insert(${DTO} dto) throws java.sql.SQLException {
java.sql.Connection con = null;
java.sql.PreparedStatement ps = null;
String sql = "insert into ${table}(${col1},${col2}) values (?,?)";
try {
con = DBManager.getConnection();
ps = con.prepareStatement(sql);
ps.set${Type1}(1, dto.get${col1Value});
ps.set${Type2}(2, dto.get${col2Value});
return ps.executeUpdate();
} catch (java.sql.SQLException e) {
StringBuilder sb = new StringBuilder(${100});
sb = sb.append("${enclosing_type}.insert()").append(" :: ");
sb = sb.append("Database insertion failed.").append(System.getProperty("line.separator"));
sb = sb.append(" Table = ${table}").append(" :: ");
sb = sb.append(" ${col1} = ").append(dto.get${col1Value});
sb = sb.append(",").append(" ${col2} = ").append(dto.get${col2Value});
logger.error(sb.toString());
throw e;
} finally {
try {
if(ps != null) {
ps.close();
ps = null;
}
if(con != null) {
con.close();
}
} catch(java.sql.SQLException e) {
// ignore
}
}
}
For that, eclipse provides inbuilt templates for writing for loops, iterator loops, main method, try/catch blocks, and many more which saves hail lot of time.
Not only that but eclipse also provides a facility to write our own customized templates.
So here I am adding my custom templates.. I will keep on adding here as whenever I find repetitive code and also get chance to write new ones.
Starting with DAO Insert method:
public int insert(${DTO} dto) throws java.sql.SQLException {
java.sql.Connection con = null;
java.sql.PreparedStatement ps = null;
String sql = "insert into ${table}(${col1},${col2}) values (?,?)";
try {
con = DBManager.getConnection();
ps = con.prepareStatement(sql);
ps.set${Type1}(1, dto.get${col1Value});
ps.set${Type2}(2, dto.get${col2Value});
return ps.executeUpdate();
} catch (java.sql.SQLException e) {
StringBuilder sb = new StringBuilder(${100});
sb = sb.append("${enclosing_type}.insert()").append(" :: ");
sb = sb.append("Database insertion failed.").append(System.getProperty("line.separator"));
sb = sb.append(" Table = ${table}").append(" :: ");
sb = sb.append(" ${col1} = ").append(dto.get${col1Value});
sb = sb.append(",").append(" ${col2} = ").append(dto.get${col2Value});
logger.error(sb.toString());
throw e;
} finally {
try {
if(ps != null) {
ps.close();
ps = null;
}
if(con != null) {
con.close();
}
} catch(java.sql.SQLException e) {
// ignore
}
}
}
No comments:
Post a Comment