Created
March 8, 2016 10:14
-
-
Save dantin/b75e439c6126807ed20f to your computer and use it in GitHub Desktop.
JDBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Employee getEmployeeById(long id) { | |
Connection conn = null; | |
PreparedStatement stmt = null; | |
ResultSet rs = null; | |
try { | |
conn = dataSource.getConnection(); | |
stmt = conn.prepareStatement( | |
"select id, firstname, lastname, salary from " + | |
"employee where id=?"); | |
stmt.setLong(1, id); | |
rs = stmt.executeQuery(); | |
Employee employee = null; | |
if (rs.next()) { | |
employee = new Employee(); | |
employee.setId(rs.getLong("id")); | |
employee.setFirstName(rs.getString("firstname")); | |
employee.setLastName(rs.getString("lastname")); | |
employee.setSalary(rs.getBigDecimal("salary")); | |
} | |
return employee; | |
} catch (SQLException e) { | |
} finally { | |
if(rs != null) { | |
try { | |
rs.close(); | |
} catch(SQLException e) {} | |
} | |
if(stmt != null) { | |
try { | |
stmt.close(); | |
} catch(SQLException e) {} | |
} | |
if(conn != null) { | |
try { | |
conn.close(); | |
} catch(SQLException e) {} | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment