classic code review
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import entity.UserInfo;
import util.DBConnection; //DAO:Data Access Object
//完成对表userinfo的增删改查(CURD)功能
public class UserInfoDAO {
// 查询全部
public List<UserInfo> selectAll() throws SQLException {
List<UserInfo> users = new ArrayList<UserInfo>();
String sql = "select * from userinfo"; // 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建Statement,执行SQL语句
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql); // 3. 处理结果集
while (rs.next()) {
UserInfo user = new UserInfo();
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
user.setBirthday(new Date(rs.getDate("birthday").getTime())); users.add(user);
}
// 4. 释放资源
rs.close();
stmt.close();
connection.close();
return users;
} public UserInfo selectByName(String name) throws SQLException { String sql = "select * from userinfo where name='" + name + "'"; // 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建Statement,执行SQL语句
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql); // 3. 处理结果集\
UserInfo user = null;
if (rs.next()) {
user = new UserInfo();
user.setId(rs.getInt("userid"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
user.setBirthday(rs.getDate("birthday")); }
// 4. 释放资源
rs.close();
stmt.close();
connection.close();
return user;
} // 按条件查询
public List<UserInfo> selectBySex(String sex) throws SQLException {
List<UserInfo> users = new ArrayList<UserInfo>();
String sql = "SELECT * FROM userinfo WHERE sex=?"; // 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建Statement,执行SQL语句
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, sex);
ResultSet rs = pstmt.executeQuery(sql); // 3. 处理结果集
while (rs.next()) {
UserInfo user = new UserInfo();
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
user.setBirthday(new Date(rs.getDate("birthday").getTime())); users.add(user);
}
// 4. 释放资源
rs.close();
pstmt.close();
connection.close();
return users;
} // 增加
public int insert(UserInfo user) throws SQLException {
String sql = "insert into userinfo(name,password,age,sex,birthday) values(?,?,?,?,?)";
// 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建PreparedStatement
PreparedStatement pstmt = connection.prepareStatement(sql); // 3. 给PreparedStatement的参数赋值
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getSex());
pstmt.setDate(5, new java.sql.Date(user.getBirthday().getTime())); // 4. 执行SQL语句
int num = pstmt.executeUpdate(); // 5. 释放资源
pstmt.close();
connection.close();
return num;
} // 修改密码
public int update(String name, String password) throws SQLException {
String sql = "update userinfo set password=? where name=?";
// 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建PreparedStatement
PreparedStatement pstmt = connection.prepareStatement(sql); // 3. 给PreparedStatement的参数赋值
pstmt.setString(1, password);
pstmt.setString(2, name); // 4. 执行SQL语句
int num = pstmt.executeUpdate(); // 5. 释放资源
pstmt.close();
connection.close();
return num;
} public int delete(String name) throws SQLException {
String sql = "delete from userinfo where name=?";
// 1. 获取数据库连接
Connection connection = DBConnection.getConnection(); // 2. 创建PreparedStatement
PreparedStatement pstmt = connection.prepareStatement(sql); // 3. 给PreparedStatement的参数赋值
pstmt.setString(1, name); // 4. 执行SQL语句
int num = pstmt.executeUpdate(); // 5. 释放资源
pstmt.close();
connection.close();
return num;
} public static void main(String[] args) {
UserInfoDAO dao = new UserInfoDAO();
try {
List<UserInfo> users = dao.selectAll();
System.out.println(users);
users = dao.selectBySex("男");
System.out.println(users); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("1998-01-01");
UserInfo user = new UserInfo(3, "niit", "123456", 18, "男", date);
int num = dao.insert(user);
if (num >= 0) {
System.out.println("插入成功");
}
} catch (SQLException | ParseException e) {
e.printStackTrace();
}
}
}
1.格式不标准
2.注释不够详细具体
1.输入的时候,不知道输入的是否数字呢,怎么能用 sc.nextDouble()来获取浮点数呢?
2.判断数字的正则表达式好像有点问题
classic code review的更多相关文章
- 我们是怎么做Code Review的
前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...
- Code Review 程序员的寄望与哀伤
一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...
- Git和Code Review流程
Code Review流程1.根据开发任务,建立git分支, 分支名称模式为feature/任务名,比如关于API相关的一项任务,建立分支feature/api.git checkout -b fea ...
- 如何搭建开源code review gerrit服务器
搭建环境:Ubuntu 14.04 一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.ora ...
- Code Review Tools
Code Review中文应该译作“代码审查”或是“代码评审”,这是一个流程,当开发人员写好代码后,需要让别人来review一下他的代码,这是一种有效发现BUG的方法.由此,我们可以审查代码的风格.逻 ...
- code review作业
下面是对结对编程队友12061166 宋天舒的code review 五个优点: 1.代码的风格优秀,注释不多,但是必要的注释还是有的,比如: // 三种模式 // mode1仅统计单个单词 // m ...
- 15个最佳的代码评审(Code Review)工具
代码评审可以被看作是计算机源代码的测试,它的目的是查找和修复引入到开发阶段的应用程序的错误,提高软件的整体素质和开发者的技能.代码审查程序以各种形式,如结对编程,代码抽查等.在这个列表中,我们编制了1 ...
- Code Review 五问五答
Code Review 是什么? Code Review即代码审查,程序猿相互审核对方的代码. Code Review能获得什么好处? 提高代码可维护性 你写的代码不再只有编译器看了,你得写出审核人能 ...
- 大家是怎么做Code Review的?
先说说我们公司现在的做法,一个团队被人为地分为两个阵营:Senior Developers和Junior Developers,比例差不多是1:1,Senior Developers就担负着对Juni ...
随机推荐
- Python爬虫——西刺
一直对爬虫这块蛮感兴趣的,所以花了点时间看了看,写了个小脚本 代码可能有点乱,毕竟Python小白,勿喷…… 嗯,话不多说,放码出来 # -*- coding: UTF-8 -*- import re ...
- SoapUI之http接口测试
SoapUI是一个开源测试工具,可以进行webservice/http协议的功能.负载.安全性测试.SoapUI pro是SoapUI的商业非开源版本,实现的功能会更多一点.一般的测试场景,用开源版本 ...
- Java中的异常处理与抛出
一.异常处理 程序运行过程中出现的,导致程序无法继续运行的错误叫做异常. Java中有多种异常,所有异常的父类是Throwable,他主要有两个子类Error和Exception. Error一般是J ...
- 【基于微信小程序的社区电商平台】Alpha迭代心得
项目团队:小豆芽 开发周期:11.5-12.2(Alpha版本) 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决问题:当前电商平台卖家买家角 ...
- 界面控件DevExpress发布v18.2.7,新版全新出发|附下载
DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...
- Thread类线程结束会唤醒使用其对象做锁而睡眠的线程
首先回顾一下我们的基础知识. sleep: 线程睡眠,不会释放锁 wait: 线程等待.释放锁. notity: 唤醒随机一个当前对象等待的线程,并不会释放锁 notityAll: 唤醒所有当前对象等 ...
- L342 Air Pollution Is Doing More Than Just Slowly Killing Us
Air Pollution Is Doing More Than Just Slowly Killing Us In the future, the authorities might need to ...
- 国内第一本cbuilder开发web的宝典震撼登场(delphi也可参考)
uniGUI入门到精通cBuilder版 众所周知,cbuilder和delphi是曾经风靡多年的强悍的万能的开发工具,岂奈大浪淘沙,B/S时代的到来让这对孪生兄弟隐姓埋名.然,无数风流人物依然对他们 ...
- python爬取post请求Reque Payload的json数据
import requests,json url = "https://www.yijiupi.com/v31/Product/ListProduct" headers = { ' ...
- c++单链表冒泡排序(交换结点),链表增删改查,运算符重载
#include <iostream> #include <stdlib.h> #include <time.h> #include <fstream> ...