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 ...
随机推荐
- 前端开发面试题总结之——JAVASCRIPT(三)
___________________________________________________________________________________ 相关知识点 数据类型.运算.对象 ...
- APP压力稳定性测试之monkey环境搭建
一.搭建adb环境: 需要的安装软件包可以使用我分享的,链接:https://pan.baidu.com/s/13DThDtc0GALabTakshcLfg 密码:0kuo:也可以自己百度下载 1)下 ...
- Unity资源内存管理--webstream控制
一 使用前提 1,需要使用资源热更新 2,使用Assetbundle资源热更(AssetBundle是产生webstream的元凶) 二 为什么要用AssetBundle AssetBundle本质上 ...
- [转]MYSQL 创建存储过程
MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...
- springboot入门学习-helloworld实例
今天学习了springboot,发现使用springboot创建项目确实非常方便,创建第一个springboot项目之前先看一下下面两个问题. 一.什么是springboot? Spring Boot ...
- DevExpress WinForms使用教程:皮肤颜色和LookAndFeel
[DevExpress WinForms v18.2下载] v18.2版本中更改了控制背景颜色和皮肤一起处理的方式.在v18.1中引入了Project Settings页面,其中包含一个skin se ...
- tensorFlow可以运行的代码
折腾了很久,终于运行成功. 才云科技的书不错,就是需要微调一二. 心得:1,记得activate tensorflow,然后再python 2,Python的代码格式很重要,不要错误. 3,还不清楚如 ...
- Android中获取文件路径的方法总结及对照
最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...
- 斐讯K2 22.5.9固件刷华硕固件实测教程
斐讯K2最新的固件是V22.5.9.163这个版本是锁死了,不能刷机的,而且不能降级到原来的可以刷机的老版本固件,也就不能刷第三方固件了,怎么破呢?下面就教大家怎么降级刷机到V22.4.2.8固件. ...
- Struts2配合layui多文件上传--下载
先说上传: 前台上传文件的js代码: var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testLi ...