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. IDEA解决SVN更新冲突

    在有冲突的文件上右键-> subversion ->resolve Text Confict->merge 将代码合并.

  2. js中三种弹出框

    javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prompt()来获得,可以利用这些对话框来完成js的输入和输出,实现与用户能进行交互的js代码 ...

  3. 递归----Python

    #递归不仅仅是学习python中会遇到的一些问题,在学习每一个语言的过程中都会遇到递归.使用递归可以让复杂的循环变得简单. 递归:程序调用自身的行为 1.写一个数的阶乘 #递归 def factor( ...

  4. Java 平时作业七

    以下是几本计算机书籍的基本信息 编号  书名         价格      出版社 1  JAVA 基础   32   清华大学出版社 2  JAVA WEB 开发  40   电子工业出版社 3  ...

  5. java 构造方法详解

    构造方法(构造器)    是一种特殊的方法,该方法只有功能:构造对象    特点:        1.没有返回值        2.构造方法的名称一定和类名一致        3.不能在构造方法中写r ...

  6. java中的常用特殊字符

    1.转义字符反斜杠(\) 我们知道html中大都是双标签,如果在标签内想要输出带有标签结束符的文本都必须进行转义,html中是采用对应的字符替换,如<可用<替换 在java当中,我们要转义 ...

  7. logging dictconfig

    #coding: utf-8 import logging import logging.config   class SingleLevelFilter(object):     def __ini ...

  8. js隐藏字符串中间部分

    在进行web前端页面开发中,有时需要从后台获取用户数据来显示在前台页面,但是考虑到用户信息安全的问题,就需要对这些信息进行处理,使其不完全显示出来,例如姓名,两个字的显示姓,名字用*代替,电话前三位和 ...

  9. python基础17_列表推导式 vs 生成器表达式

    [ ] 列表推导式,是用简单的语法来生成列表, ( ) 生成器表达式,是用简单的语法创建个生成器. 外观上仅括号不一样. 虽然写起来方便,但是读起来稍显费力,另外,不易调试. # 列表推导式 prin ...

  10. 字符串a-b

    #include<iostream> #include<stdio.h> #include<algorithm> #include<cmath> #in ...