my 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.部分定义的变量难以理解

时间: 2024-08-06 20:31:43

my code review的相关文章

后端code review finished 12-28

今天只有天赋和士杰有相应的后端代码的code review工作,因为并没有召开daily scrum.只是天赋和士杰对后端所有的代码进行了review,对代码进行了整理并删除了一些残留的0 refrence的代码.

code review的目的

Code review 是系统的检查程序源码,目的是在初始开发阶段找到和修正错误,提高软件质量和开发人员的技术水平. Java源码的Code review包括哪些那: 1.编程规范 2.面向对象设计检查 3.性能检查 4.资源管理:内存泄露 5.线程安全:多线程,死锁 6.处理流程:条件语句,循环结构 7.异常处理 8.数据库 有许多帮忙我们检查代码的自动化工具:比如PMD工具,http://pmd.sourceforge.net/pmd-5.1.1/ PMD可以帮我做的: PMD scans

我是如何进行code review的

众所周知,代码审查是软件开发过程中十分重要的环节,楼主结合自己的实际工作经验,和大家分享一下在实际工作中代码审查是如何开展的, 笔者水平有限,若有错误和纰漏,还请大家指正. 代码审查的阻力 我想不通公司不同部门对代码审查这项工作的重视程度还是不一样的,对于代码审查的阻力总结了以下几点: 国内的整体环境,国内的公司,尤其是互联网公司,讲究速度致上,软件开发的迭代周期周期短,速度快,因为竞争太大,开发的产品要求快速上线,对代码审查不是很重视,先上线,出了问题再解决. 公司的规模,大公司重视流程,把代

使用RBTool自动提交code review请求

使用RBTool自动提交code review请求 前言 让我们回想一下手工提交review请求的过程: 首先得用 svn diff > filename.diff 生成diff文件. 然后输入review board的网址,可能是 rb.companyname.com 然后需要输入你的账号密码进行登录验证. 然后你需要填写你的svn repository 地址,然后上传diff文件. 然后你进到review请求的详细页面,填写summary, description, test-done, g

如何搭建gerrit开源code review工具

搭建环境:Ubuntu 14.04 一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 安装:sudo tar zxvf ./jdk-7u79-linux-x64.tar.gz -C /opt 配置:vim ~/.bashrc export JAV

如何在python脚本开发做code review

在软件项目开发中,我们经常提到一个词"code review".code review中文翻译过来就是代码评审或复查,简而言之就是编码完成后由其他人通过阅读代码来检查代码的质量(可编译.可运行.可读.可维护.可复用),这些性质都比较抽象,但是一般都可以通过以下的检查点来实现: 检查代码的命名方式是否符合规范,代码的可读和可维护必须要求所有参与编码的同事使用的命名有统一的规范(注意每个人有自己的代码风格,但是要符合可读性的代码规范): 检查代码的注释,注释一般包括:1.类要有类用途和使用

iOS从零开始 Code Review

http://www.cocoachina.com/ios/20151117/14208.html 这篇帖子不是通篇介绍Code Review的方法论, 而是前大段记录了我们团队怎么从没有这个习惯到每天都进行review的过程, 后小段给出了我的一些建议. 希望能对诸位的团队有所帮助. 最初来到这个新组建的团队是木有code review的. 头说, 这个月你来搞吧. 当我第一次知道必须得搞review的时候, 其实我是拒绝的! 因为我觉得…呀…你不能叫我马上搞立马搞, 第一, 我要试一下, 我

Friend&#39;s Code Review

代码: ``` package lservice; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts

Code Review for SSIS package

以下是我对SSIS包进行code review的一些建议,如果有其他更好的方案欢迎拍砖. A. 查看是否使用了最优的解决方案 1. 最优的结构视图 2. 解决方案,包,任务,组建,参数的命名使用了易读的命名方式 3. 遵循了最优的设计,优化,调整方案 B. 配置 查看是否所有的配置已经成功,并且能够从外部和父包中获得正确的配置信息. C. 查看能否通过以下测试 1. 正常的场景 查看到所有的表数据/文件已经生成并且是正确的 查看所有的数据在表中没有被截断或有不需要的空格/字符 重新执行包,看是否

iOS 代码审查:宽松的指导方针(iOS Code Review: Loose Guidelines)

iOS 代码审查:宽松的指导方针 (iOS Code Review: Loose Guidelines) Jack Nutting February 19, 2014 IOS + From time to time I've been asked to do an independent code review, to determine the overall health of a client's code base. Unlike a code walkthrough, where so