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的更多相关文章

  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. 使用eclipse新建一个c项目

    一.打开eclipse并新建项目 1.快捷键:字体放大:Ctrl+Shift+“+” 字体缩小:Ctrl+“-”  

  2. 随机函数rand()与srand()

    一.int rand(void); 函数所在的头文件是stdlib.h: 其内部实现线性同除法,不是真正的随机数.通常rand()%x是指在x范围内取模,返回值0-x; 系统默认随机种子是1: 二.v ...

  3. Android串口屏(电阻,电容触摸),带AV输入,7寸LCD1(800*48...

    基本参数:CPU:MT6572 双核1GHzRAM:512MB存储:4GB网络:GSM,WCDMA(BAND1)WIFI:2.4G 802.11bgn蓝牙:2.0支持GPS定位 扩展参数:1.电源输入 ...

  4. java 的重写(覆盖) 和重载的区别

    方法的的重写(覆盖) 在类继承中,子类可以修改从父类继承来的行为,也就是说子类能创建一个与父类方法有不同功能的方法,但具有相同的:名称.返回类型.参数列表.如果在子类中定义一个方法,其方法名称.返回值 ...

  5. UVa-156 Ananagrams(map映射)

    #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> ...

  6. 踩坑学习python自动化测试第二天!

    class put_out(object): def Helloword(str): print(str) Hello,Inder,Pist ="", [],{} word= st ...

  7. shell中的数据生命周期scope

    #!/bin/shexit 0#shell 中, 默认所有的变量都是 全局变量,除非主动变量前面加 local 修饰#shell 变量是字符变量,只能放字符和数字,shell数组也是如此;而数字也是图 ...

  8. Shell bash 数学运算 bc

    1.bc命令可以完成浮点数的运算.其中 scale可以指定保留的小数点位数. 2.举例 例1: 例2:

  9. mysql 的存储过程_多字段

    mysql 的存储过程 一.准备工作 新建一个表 /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server V ...

  10. Python成长之路【第二篇】Python基础之数据类型

    阅读目录 简介 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(整形,长整形,浮点型 ...