1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public abstract class BaseDao implements Serializable {
private transient static Connection connection;
static {
try {
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/db");
connection = ds.getConnection();
} catch (NamingException ex) {
} catch (SQLException ex) {
}
}
protected Connection con;
protected transient final ResourceBundle requests;
protected transient final Logger log = Logger.getLogger(BaseDao.class);
protected transient final ResourceBundle msg = ResourceBundle.getBundle("msg");
public BaseDao() throws SQLException {
this.requests = ResourceBundle.getBundle("requests");
this.con = BaseDao.connection;
}
protected PreparedStatement prepareStatement(final String requestName) throws SQLException {
checkConnection();
return con.prepareStatement(requests.getString(requestName));
}
protected PreparedStatement prepareRawStatement(final String request) throws SQLException {
checkConnection();
return con.prepareStatement(request);
}
protected PreparedStatement prepareStatementReturnId(final String requestName) throws SQLException {
checkConnection();
return con.prepareStatement(requests.getString(requestName), Statement.RETURN_GENERATED_KEYS);
}
private void checkConnection() throws SQLException {
if (this.con == null) {
this.con = getConnection();
}
}
private Connection getConnection() throws SQLException {
DataSource ds;
try {
Context initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/db");
return ds.getConnection();
} catch (NamingException ex) {
return null;
}
}
} |
Partager