完成整个DAO的实现及测试代码

 1 package cn.itcast.domain;
 2
 3 import java.util.Date;
 4
 5 public class User {
 6
 7     private int id;
 8     private String name;
 9     private Date birthday;
10     private float money;
11
12     public int getId() {
13         return id;
14     }
15
16     public void setId(int id) {
17         this.id = id;
18     }
19
20     public String getName() {
21         return name;
22     }
23
24     public void setName(String name) {
25         this.name = name;
26     }
27
28     public Date getBirthday() {
29         return birthday;
30     }
31
32     public void setBirthday(Date birthday) {
33         this.birthday = birthday;
34     }
35
36     public float getMoney() {
37         return money;
38     }
39
40     public void setMoney(float money) {
41         this.money = money;
42     }
43
44 }

User

 1 package cn.itcast.dao;
 2
 3 import java.sql.SQLException;
 4
 5 import cn.itcast.domain.User;
 6
 7 public interface UserDao {
 8
 9     public void addUser(User user) throws SQLException;
10
11     public User getUser(int userId);
12
13     public void update(User user);
14
15     public void delete(User user);
16
17     public User findUser(String userName,String password);
18
19
20
21 }

UserDao

  1 package cn.itcast.dao.impl;
  2
  3 import java.sql.Connection;
  4 import java.sql.Date;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9
 10 import cn.itcast.dao.UserDao;
 11 import cn.itcast.domain.User;
 12 import cn.itcast.exception.DaoException;
 13 import cn.itcast.jdbc.jdbcUtils;
 14
 15 public class UserDaoJdbcImpl implements UserDao {
 16
 17     @Override
 18     public void addUser(User user) throws DaoException {
 19
 20         Connection conn = null;
 21         PreparedStatement ps = null;
 22         ResultSet rs = null;
 23
 24         try {
 25             // 建立连接
 26             conn = jdbcUtils.getConnection();
 27
 28             String sql = "insert into user(name,birthday,money) values(?,?,?)";
 29             ps = conn.prepareStatement(sql);
 30
 31             ps.setString(1, user.getName());
 32             ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
 33             ps.setFloat(3, user.getMoney());
 34
 35             ps.executeUpdate();
 36
 37         } catch (SQLException e) {
 38             throw new DaoException(e.getMessage(), e);
 39         } finally {
 40             jdbcUtils.free(rs, ps, conn);
 41         }
 42
 43     }
 44
 45     @Override
 46     public User getUser(int userId) {
 47         Connection conn = null;
 48         PreparedStatement ps = null;
 49         ResultSet rs = null;
 50         User user = null;
 51
 52         try {
 53             conn = jdbcUtils.getConnection();
 54             String sql = "select id,name,money,birthday from user where id = ?";
 55             ps = conn.prepareStatement(sql);
 56             ps.setInt(1, userId);
 57             rs = ps.executeQuery();
 58
 59             while (rs.next()) {
 60
 61                 user = mappingUser(rs);
 62
 63             }
 64
 65         } catch (SQLException e) {
 66             throw new DaoException(e.getMessage());
 67         } finally {
 68             jdbcUtils.free(rs, ps, conn);
 69         }
 70
 71         return user;
 72     }
 73
 74     public User mappingUser(ResultSet rs) throws SQLException {
 75         User user = new User();
 76         user.setId(rs.getInt("id"));
 77         user.setName(rs.getString("name"));
 78         user.setMoney(rs.getFloat("money"));
 79         user.setBirthday(rs.getDate("birthday"));
 80         return user;
 81     }
 82
 83     @Override
 84     public void update(User user) {
 85         Connection conn = null;
 86         PreparedStatement ps = null;
 87         ResultSet rs = null;
 88
 89         try {
 90             conn = jdbcUtils.getConnection();
 91             String sql = "update user set name = ?,money=? where id=?";
 92             ps = conn.prepareStatement(sql);
 93             ps.setString(1, user.getName());
 94             //ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
 95             ps.setFloat(2, user.getMoney());
 96             ps.setInt(3, user.getId());
 97
 98             ps.executeUpdate();
 99
100         } catch (SQLException e) {
101             new DaoException(e.getMessage());
102         } finally {
103             jdbcUtils.free(rs, ps, conn);
104         }
105
106     }
107
108     @Override
109     public void delete(User user) {
110
111         Connection conn = null;
112         Statement st = null;
113         ResultSet rs = null;
114
115         try {
116             conn = jdbcUtils.getConnection();
117             st = conn.createStatement();
118             String sql = "delete from user where id=" + user.getId();
119             st.executeUpdate(sql);
120
121         } catch (SQLException e) {
122             throw new DaoException(e.getMessage());
123         } finally {
124             jdbcUtils.free(rs, st, conn);
125         }
126
127     }
128
129     @Override
130     public User findUser(String userName, String password) {
131
132         Connection conn = null;
133         PreparedStatement ps = null;
134         ResultSet rs = null;
135         User user = null;
136
137         try {
138             conn = jdbcUtils.getConnection();
139             String sql = "select id,name,money,birthday from user where name = ?";
140             ps = conn.prepareStatement(sql);
141             ps.setString(1, userName);
142             rs = ps.executeQuery();
143
144             while (rs.next()) {
145                 user = mappingUser(rs);
146             }
147
148         } catch (SQLException e) {
149             throw new DaoException(e.getMessage());
150         } finally {
151             jdbcUtils.free(rs, ps, conn);
152         }
153
154         return user;
155     }
156
157 }

UserDaoJdbcImpl

 1 package cn.itcast.exception;
 2
 3 public class DaoException extends RuntimeException {
 4
 5     private static final long serialVersionUID = 1L;
 6
 7     public DaoException() {
 8         super();
 9         // TODO Auto-generated constructor stub
10     }
11
12     public DaoException(String message, Throwable cause) {
13         super(message, cause);
14         // TODO Auto-generated constructor stub
15     }
16
17     public DaoException(String message) {
18         super(message);
19         // TODO Auto-generated constructor stub
20     }
21
22     public DaoException(Throwable cause) {
23         super(cause);
24         // TODO Auto-generated constructor stub
25     }
26
27 }

DaoException

 1 package cn.itcast.jdbc;
 2
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8
 9 public class jdbcUtils {
10
11     private static String url = "jdbc:mysql://localhost:3306/jdbc";
12     private static String user = "root";
13     private static String password = "123";
14
15     private jdbcUtils() {
16
17     }
18
19     static {
20         try {
21             Class.forName("com.mysql.jdbc.Driver");
22         } catch (ClassNotFoundException e) {
23             e.printStackTrace();
24         }
25     }
26
27     public static Connection getConnection() throws SQLException {
28         return DriverManager.getConnection(url, user, password);
29     }
30
31     public static void free(ResultSet rs, Statement st, Connection conn) {
32
33         try {
34             if (rs != null)
35                 rs.close();
36         } catch (SQLException e) {
37             e.printStackTrace();
38         } finally {
39
40             try {
41                 if (st != null)
42                     st.close();
43             } catch (SQLException e) {
44                 e.printStackTrace();
45             } finally {
46
47                 try {
48                     if (conn != null)
49                         conn.close();
50                 } catch (SQLException e) {
51                     e.printStackTrace();
52                 }
53             }
54
55         }
56
57     }
58 }

jdbcUtils

 1 package cn.itcast.dao;
 2
 3 import java.sql.SQLException;
 4 import java.util.Date;
 5
 6 import cn.itcast.dao.impl.UserDaoJdbcImpl;
 7 import cn.itcast.domain.User;
 8
 9 public class UserTest {
10
11     /**
12      * @param args
13      * @throws SQLException
14      */
15     public static void main(String[] args) throws SQLException {
16
17         UserDao userDao = new UserDaoJdbcImpl();
18
19         //User user = new User();
20         //user.setBirthday(new Date());
21         //user.setName("dao name1");
22         //user.setMoney(1000.0f);
23
24 //        userDao.addUser(user);
25 //
26 //        User u = userDao.findUser(user.getName(),null);
27 //        System.out.println(u.getId());
28
29         User u = userDao.getUser(1);
30
31         u.setMoney(2000.0f);
32         userDao.update(u);
33
34         User u1 = userDao.getUser(1);
35         userDao.delete(u1);
36
37     }
38
39 }

UserTest

时间: 2024-12-22 22:11:41

完成整个DAO的实现及测试代码的相关文章

测试代码

   编写函数或类时,还可以为其编写测试.通过测试,可确定代码面对各种输入都能够按照要求那样工作. 单元测试和测试用例:   单元测试用于核实蛮熟的某个方面没有问题:测试用例是一组单元测试,这些单元测试一起核实函数在各种情形下的行为都符合要求. 良好的测试用例考虑到了函数可能收到的各种输入,包含针对这些所有情形的测试. 全覆盖测试用例包含一整套单元测试,涵盖了各种可能的函数使用方式.对于大型项目,要实现覆盖可能很难.所以通常,最初只要针对 代码的重要行为编写测试即可,等项目被广泛使用率再考虑全覆

第4次作业类测试代码+105032014166+张珍珍

第4次作业:准备类测试代码 类测试代码的具体要求如下: (1)设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不限于此: 完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c) 要求: 1.        画出类图: 2.        完成界面和相应的功能

Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)

MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明文是yanzi1225627,得到MD5加密后的字符串是:14F2AE15259E2C276A095E7394DA0CA9  但不能由后面一大串倒推出yanzi1225627.因此可以用来存储用户输入的密码在服务器上.现在下载文件校验文件是否中途被篡改也是用的它,原理参见:http://blog.c

MyPython-->进阶篇-->测试代码

测试函数 要学习测试,得要有测试的代码.下面是一个简单的函数,接受名和姓并返回整洁的姓名 name_function.py def get_allname(x,m): allname = ('%s %s'%(x,m)).title() return allname 编写测试代码 from name_function import get_allname print(get_allname('cc','leo')) import unittest class NameTestCase(unitte

x264测试代码

建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h"  //如果没有,下载地址为:http://download.csdn.net/detail/evsqiezi/7014021extern "C"{#include "x264.h"#include "x264_config.h"};usi

Maven配置插件跳过测试代码的编译和运行

Maven配置插件跳过测试代码的编译和运行: <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</targe

linux下libphenom的测试代码

使用说明:测试使用libphenom库的字符串追加函数,效率是strcat的60多倍.所以在进行大量的字符串累加的时候可以考虑使用libphenom库  依赖库: ck-0.4.5.tar.gz cmake-3.1.2.tar.gz libtap-1.12.0.tar.bz2 libphenom.tar.gz 头文件: #include <phenom/sysutil.h> #include <phenom/string.h> #include <phenom/stream.

国嵌内核驱动进阶班-7-1(Ioctl设备控制)--- 测试代码

驱动内容: 1 #include <linux/module.h> 2 #include <linux/types.h> 3 #include <linux/fs.h> 4 #include <linux/errno.h> 5 #include <linux/mm.h> 6 #include <linux/sched.h> 7 #include <linux/init.h> 8 #include <linux/cde

测试代码高亮

测试方法 测试代码 测试方法 测试代码 源代码,按Markdown语法进行默认的代码高亮的效果: let g:octopress_path = "path/to/dir" 使用octopress的代码高亮进行显示的效果: 测试语法高亮 1 let g:octopress_path = "path/to/dir" 网上的一个例子 Discover if a number is primeSource Article 1 2 3 4 5 class Fixnum def