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 ...
随机推荐
- maven安装与基本配置
maven安装与基本配置 依赖:java环境,JDK安装 一. maven安装 (一)下载maven (二)安装与环境变量设置 (三)maven setting.xml配置 二. 创建maven项目 ...
- SpringBoot配置ActiveMQ
1.添加依赖 <!-- activeMQ --> <dependency> <groupId>org.springframework.boot</groupI ...
- asp调用短信接口实现用户注册
前几天做一个asp语言开发的网站需要实现用户注册短信验证功能,就研究了一下如何实现,简单给大家分享下调用过程. 首先需要找到一个第三方短信接口,当时用的是动力思维乐信的短信接口. 首先需要先注册个动力 ...
- Python随笔--对象
组合的用法:
- 指导手册06:HBase安装部署
指导手册06:HBase安装部署 配置环境 1.参考文件: https://www.cnblogs.com/lzxlfly/p/7221890.html https://www.cnblogs.com ...
- 俄罗斯方块win c 图形线程
环境准备:visual stdio 2015,easyx, //
- Delphi10.2 关于Http 下载
演示如图: 代码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Vari ...
- 怎样将virtualbox中的虚拟系统安装到c盘以外的盘
首先在安装的时候是可以选择虚拟机文件的位置的,如果当时没注意,只能现在移动一下了,很简单 首先将 C:/Users目录下的.VirtualBox和VirtualBox VMs两个文件拷贝到你愿意放的位 ...
- 配置Nim的默认编译参数 release build并运行
配置Nim的默认编译参数 release build并运行 默认情况下nim编译是debug build,如果需要release build, 需要加上-d:release , release编译的命 ...
- 2019-04-26-day041-数据库的索引
内容回顾 多表查询 联表查 内连接 左右两表中能连上的行才被保留 表1 inner join 表2 on 表1.字段1=表2.字段2 外连接 左外连接 表1中所有的项都会被保留,而表2中只有匹配上表1 ...