Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 29, 2015 14:20
Show Gist options
  • Save SeongUgJung/b3a865268d10065f1d10 to your computer and use it in GitHub Desktop.
Save SeongUgJung/b3a865268d10065f1d10 to your computer and use it in GitHub Desktop.
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
public class JDBCEmployeeDAOImpl {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(Employee employee){
String sql = "SELECT id, first, last, age FROM Employees WHERE id = ?";
jdbcTemplate = new JdbcTemplate(dataSource);
Employee employee = (Employee) jdbcTemplate.queryForObject(
sql, new Object[] { id }, new EmployeeRowMapper());
}
}
public class JdbcCustomerDAO extends JdbcDaoSupport {
public void insert(Customer customer){
 
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
 
getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
customer.getName(),customer.getAge()
});
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment