Chinaunix首页 | 论坛 | 博客
  • 博客访问: 673742
  • 博文数量: 779
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 5000
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 13:28
文章分类

全部博文(779)

文章存档

2011年(1)

2008年(778)

我的朋友

分类:

2008-10-27 13:28:23

 /**
* Created by IntelliJ IDEA.

* User: leizhimin

* Date: 2008-4-22 11:40:18

* Note: 子类DAO
*/

public class UserDAO extends BaseDAO implements IUserDAO {
    public void insert(User user) {
        String name = user.getName();
        int age = user.getAge().intValue();

        Connection conn = null;
        Statement stmt = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            stmt.execute("INSERT INTO user (name,age) " + "VALUES('" + name + "'," + age + ")");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public User find(Integer id) {
        Connection conn = null;
        Statement stmt = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();

            ResultSet result = stmt.executeQuery(
                    "SELECT * FROM user WHERE id=" + id.intValue());
            if (result.next()) {
                Integer i = new Integer(result.getInt(1));
                String name = result.getString(2);
                Integer age = new Integer(result.getInt(3));

                User user = new User();
                user.setId(i);
                user.setName(name);
                user.setAge(age);

                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
--------------------next---------------------

阅读(347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~