Last active
August 29, 2015 14:20
-
-
Save SeongUgJung/b3a865268d10065f1d10 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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(); | |
} | |
} |
This file contains hidden or 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 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()); | |
} | |
} |
This file contains hidden or 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 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