1. package dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13.  
  14. import entity.UserInfo;
  15. import util.DBConnection;
  16.  
  17. //DAO:Data Access Object
  18. //完成对表userinfo的增删改查(CURD)功能
  19. public class UserInfoDAO {
  20. // 查询全部
  21. public List<UserInfo> selectAll() throws SQLException {
  22. List<UserInfo> users = new ArrayList<UserInfo>();
  23. String sql = "select * from userinfo";
  24.  
  25. // 1. 获取数据库连接
  26. Connection connection = DBConnection.getConnection();
  27.  
  28. // 2. 创建Statement,执行SQL语句
  29. Statement stmt = connection.createStatement();
  30. ResultSet rs = stmt.executeQuery(sql);
  31.  
  32. // 3. 处理结果集
  33. while (rs.next()) {
  34. UserInfo user = new UserInfo();
  35. user.setName(rs.getString("name"));
  36. user.setPassword(rs.getString("password"));
  37. user.setAge(rs.getInt("age"));
  38. user.setSex(rs.getString("sex"));
  39. user.setBirthday(new Date(rs.getDate("birthday").getTime()));
  40.  
  41. users.add(user);
  42. }
  43. // 4. 释放资源
  44. rs.close();
  45. stmt.close();
  46. connection.close();
  47. return users;
  48. }
  49.  
  50. public UserInfo selectByName(String name) throws SQLException {
  51.  
  52. String sql = "select * from userinfo where name='" + name + "'";
  53.  
  54. // 1. 获取数据库连接
  55. Connection connection = DBConnection.getConnection();
  56.  
  57. // 2. 创建Statement,执行SQL语句
  58. Statement stmt = connection.createStatement();
  59. ResultSet rs = stmt.executeQuery(sql);
  60.  
  61. // 3. 处理结果集\
  62. UserInfo user = null;
  63. if (rs.next()) {
  64. user = new UserInfo();
  65. user.setId(rs.getInt("userid"));
  66. user.setName(rs.getString("name"));
  67. user.setPassword(rs.getString("password"));
  68. user.setAge(rs.getInt("age"));
  69. user.setSex(rs.getString("sex"));
  70. user.setBirthday(rs.getDate("birthday"));
  71.  
  72. }
  73. // 4. 释放资源
  74. rs.close();
  75. stmt.close();
  76. connection.close();
  77. return user;
  78. }
  79.  
  80. // 按条件查询
  81. public List<UserInfo> selectBySex(String sex) throws SQLException {
  82. List<UserInfo> users = new ArrayList<UserInfo>();
  83. String sql = "SELECT * FROM userinfo WHERE sex=?";
  84.  
  85. // 1. 获取数据库连接
  86. Connection connection = DBConnection.getConnection();
  87.  
  88. // 2. 创建Statement,执行SQL语句
  89. PreparedStatement pstmt = connection.prepareStatement(sql);
  90. pstmt.setString(1, sex);
  91. ResultSet rs = pstmt.executeQuery(sql);
  92.  
  93. // 3. 处理结果集
  94. while (rs.next()) {
  95. UserInfo user = new UserInfo();
  96. user.setName(rs.getString("name"));
  97. user.setPassword(rs.getString("password"));
  98. user.setAge(rs.getInt("age"));
  99. user.setSex(rs.getString("sex"));
  100. user.setBirthday(new Date(rs.getDate("birthday").getTime()));
  101.  
  102. users.add(user);
  103. }
  104. // 4. 释放资源
  105. rs.close();
  106. pstmt.close();
  107. connection.close();
  108. return users;
  109. }
  110.  
  111. // 增加
  112. public int insert(UserInfo user) throws SQLException {
  113. String sql = "insert into userinfo(name,password,age,sex,birthday) values(?,?,?,?,?)";
  114. // 1. 获取数据库连接
  115. Connection connection = DBConnection.getConnection();
  116.  
  117. // 2. 创建PreparedStatement
  118. PreparedStatement pstmt = connection.prepareStatement(sql);
  119.  
  120. // 3. 给PreparedStatement的参数赋值
  121. pstmt.setString(1, user.getName());
  122. pstmt.setString(2, user.getPassword());
  123. pstmt.setInt(3, user.getAge());
  124. pstmt.setString(4, user.getSex());
  125. pstmt.setDate(5, new java.sql.Date(user.getBirthday().getTime()));
  126.  
  127. // 4. 执行SQL语句
  128. int num = pstmt.executeUpdate();
  129.  
  130. // 5. 释放资源
  131. pstmt.close();
  132. connection.close();
  133. return num;
  134. }
  135.  
  136. // 修改密码
  137. public int update(String name, String password) throws SQLException {
  138. String sql = "update userinfo set password=? where name=?";
  139. // 1. 获取数据库连接
  140. Connection connection = DBConnection.getConnection();
  141.  
  142. // 2. 创建PreparedStatement
  143. PreparedStatement pstmt = connection.prepareStatement(sql);
  144.  
  145. // 3. 给PreparedStatement的参数赋值
  146. pstmt.setString(1, password);
  147. pstmt.setString(2, name);
  148.  
  149. // 4. 执行SQL语句
  150. int num = pstmt.executeUpdate();
  151.  
  152. // 5. 释放资源
  153. pstmt.close();
  154. connection.close();
  155. return num;
  156. }
  157.  
  158. public int delete(String name) throws SQLException {
  159. String sql = "delete from userinfo where name=?";
  160. // 1. 获取数据库连接
  161. Connection connection = DBConnection.getConnection();
  162.  
  163. // 2. 创建PreparedStatement
  164. PreparedStatement pstmt = connection.prepareStatement(sql);
  165.  
  166. // 3. 给PreparedStatement的参数赋值
  167. pstmt.setString(1, name);
  168.  
  169. // 4. 执行SQL语句
  170. int num = pstmt.executeUpdate();
  171.  
  172. // 5. 释放资源
  173. pstmt.close();
  174. connection.close();
  175. return num;
  176. }
  177.  
  178. public static void main(String[] args) {
  179. UserInfoDAO dao = new UserInfoDAO();
  180. try {
  181. List<UserInfo> users = dao.selectAll();
  182. System.out.println(users);
  183. users = dao.selectBySex("男");
  184. System.out.println(users);
  185.  
  186. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  187. Date date = sdf.parse("1998-01-01");
  188. UserInfo user = new UserInfo(3, "niit", "123456", 18, "男", date);
  189. int num = dao.insert(user);
  190. if (num >= 0) {
  191. System.out.println("插入成功");
  192. }
  193. } catch (SQLException | ParseException e) {
  194. e.printStackTrace();
  195. }
  196. }
  197. }

1.格式不标准

2.注释不够详细具体

1.输入的时候,不知道输入的是否数字呢,怎么能用 sc.nextDouble()来获取浮点数呢?

2.判断数字的正则表达式好像有点问题

classic code review的更多相关文章

  1. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  2. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  3. Git和Code Review流程

    Code Review流程1.根据开发任务,建立git分支, 分支名称模式为feature/任务名,比如关于API相关的一项任务,建立分支feature/api.git checkout -b fea ...

  4. 如何搭建开源code review gerrit服务器

    搭建环境:Ubuntu 14.04 一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.ora ...

  5. Code Review Tools

    Code Review中文应该译作“代码审查”或是“代码评审”,这是一个流程,当开发人员写好代码后,需要让别人来review一下他的代码,这是一种有效发现BUG的方法.由此,我们可以审查代码的风格.逻 ...

  6. code review作业

    下面是对结对编程队友12061166 宋天舒的code review 五个优点: 1.代码的风格优秀,注释不多,但是必要的注释还是有的,比如: // 三种模式 // mode1仅统计单个单词 // m ...

  7. 15个最佳的代码评审(Code Review)工具

    代码评审可以被看作是计算机源代码的测试,它的目的是查找和修复引入到开发阶段的应用程序的错误,提高软件的整体素质和开发者的技能.代码审查程序以各种形式,如结对编程,代码抽查等.在这个列表中,我们编制了1 ...

  8. Code Review 五问五答

    Code Review 是什么? Code Review即代码审查,程序猿相互审核对方的代码. Code Review能获得什么好处? 提高代码可维护性 你写的代码不再只有编译器看了,你得写出审核人能 ...

  9. 大家是怎么做Code Review的?

    先说说我们公司现在的做法,一个团队被人为地分为两个阵营:Senior Developers和Junior Developers,比例差不多是1:1,Senior Developers就担负着对Juni ...

随机推荐

  1. flask 第四章 偏函数 Local空间转时间 myLocalStack RunFlask+request 请求上下文

    1 . 偏函数 (partial) from functools import partial def func(*args,**kwargs): a=args b=kwargs return a,b ...

  2. ROS机器人导航仿真(kinetic版本)

    准备工作: ubuntu 16.04系统;ROS kinetic版本;ROS包turtlebot,导航包rbx1,模拟器arbotix,可视化rviz 1.安装ubuntu 16.04系统与安装ROS ...

  3. python中的unique()

    a = np.unique(A) 对于一维数组或者列表,unique函数去除其中重复的元素,并按元素由大到小返回一个新的无元素重复的元组或者列表 import numpy as np A = [1, ...

  4. centos7 无界面静默安装 oracle

    环境准备 Centos7.3.64  64位   这里使用的是阿里云 ECS主机(1核,2G内存,40G硬盘) Oracle 11g R2 64位安装介质(版本11.2.0.1)下载地址:http:/ ...

  5. 《Swell数学》用户故事

    一.用户故事基础知识: 1. 从用户的角度来描述用户渴望得到的功能. 2. 用户故事是描述对用户有价值的功能,好的用户故事应该包括角色.功能和商业价值三个要素. 3. 一个用户故事只是以客户能够明白的 ...

  6. Django中views笔记

    reverse反解析 #路由中定义namespace.name,reverse可将其转换为url url = reverse('namespace:name') return redirect(url ...

  7. 结对作业收获_core组

    收获:编码之前必须的思考是逃不掉的,而且这一步是磨刀不误砍柴工,而且会加速以后的步骤 分析: 首要重要的事情是:需要完成的功能,理清逻辑关系.我们要随机产生一定要求的算式,并且计算出算式的值. 其次的 ...

  8. php字符串转数组

    下面代码包括了含有中文汉字的字符. function str2arr($str) { $length = mb_strlen($str, 'utf-8'); $array = []; for ($i= ...

  9. python中filter,reduce,map的用法

    filter的用法: 操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函数返回Tr ...

  10. Oracle启动和关闭数据库

    本机只安装一个数据库的情况下sqlplus / as sysdba启动数据库startup关闭数据库shutdown immediate