1: package dataSourceDemo;
2:
3: import java.sql.*;
4:
5: import javax.sql.DataSource;
6:
7: public class UserDAO implements IUserDAO {
8:
9: private DataSource dataSource;
10:
11: public User find(Integer id) {
12: // TODO 自动生成方法存根
13: return null;
14: }
15:
16: public void insert(User user) {
17: // TODO 自动生成方法存根
18: String name = user.getName();
19: int age = user.getAge().intValue();
20:
21: Connection conn = null;
22: Statement stmt =null;
23:
24: try {
25: conn = dataSource.getConnection();
26: stmt = conn.createStatement();
27: String sql = "insert into user (name, age)"+"values('"+name+"',"+age+")";
28: stmt.execute(sql);
29: }catch(Exception e) {
30: e.printStackTrace();
31: } finally {
32: if(stmt != null) {
33: try {
34: stmt.close();
35: }catch(Exception e) {
36: e.printStackTrace();
37: }
38: }
39: if(conn != null) {
40: try {
41: conn.close();
42: } catch(Exception e) {
43: e.printStackTrace();
44: }
45: }
46: }
47: }
48:
49: public DataSource getDataSource() {
50: return dataSource;
51: }
52:
53: public void setDataSource(DataSource dataSource) {
54: this.dataSource = dataSource;
55: }
56:
57: }