请老师检查

请老师检查

相关代码:商品实体类

package com.imooc.jdbc.goods.entity;

import java.util.Date;

/**
 * 商品实体类
 */
public class Goods {
    public Goods(){

    }

    private String name;
    private Float price;
    private String desp;
    private Date create_time;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getDesp() {
        return desp;
    }

    public void setDesp(String desp) {
        this.desp = desp;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date creat_time) {
        this.create_time = creat_time;
    }
}

相关代码:分页查询商品类

package com.imooc.jdbc.goods;
/**
 * 分页查询商品
 */

import com.imooc.common.DbUtils;
import com.imooc.jdbc.goods.entity.Goods;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Paginationcommand implements Command{
    @Override
    public void execute() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入页码:");
        int page=sc.nextInt();
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Goods> list = new ArrayList();
        try {
            conn=DbUtils.getConnection();
            String sql="select * from goods limit ?,5 ";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,(page-1)*5);
            rs=pstmt.executeQuery();
            while (rs.next()){
                String name = rs.getString("name");
                Float price = rs.getFloat("price");
                String desp=rs.getString("desp");
                Date create_time= rs.getDate("create_time");
                Goods goods=new Goods();
                goods.setName(name);
                goods.setPrice(price);
                goods.setDesp(desp);
                goods.setCreate_time(create_time);
                list.add(goods);
                System.out.println(name+"  "+price+"  "+desp+"  "+create_time);
            }
            //System.out.println(list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DbUtils.closeConnection(rs,pstmt,conn);
        }
    }
}

相关代码:写入商品类

package com.imooc.jdbc.goods;
/**
 * 写入类
 */

import com.imooc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class Insert implements Command {

    @Override
    public void execute() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品名称");
        String name=sc.next();
        System.out.println("请输入商品价格");
        Float price=sc.nextFloat();
        System.out.println("请输入商品描述");
        String desp=sc.next();
        System.out.println("请输入日期");
        String create_time=sc.next();
        java.util.Date udCreate_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            udCreate_time = sdf.parse(create_time);
        } catch (ParseException e){
            e.printStackTrace();
        }
        long time=udCreate_time.getTime();
        java.sql.Date sdCreate_time=new java.sql.Date(time);
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn=DbUtils.getConnection();
            String sql = "insert into goods(name,price,desp,create_time) values(?,?,?,?)";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setFloat(2,price);
            pstmt.setString(3,desp);
            pstmt.setDate(4,sdCreate_time);
            int cnt=pstmt.executeUpdate();
            if(cnt==1){
                System.out.println("成功添加"+cnt+"条");
            }else{
                System.out.println("添加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            DbUtils.closeConnection(null,pstmt,conn);
        }
    }
}

相关截图:

https://img1.sycdn.imooc.com//climg/63ff00ef0924c54809750344.jpghttps://img1.sycdn.imooc.com//climg/63ff00ef090cca0805340230.jpg


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

另外,有一个小问题可以完善一下:当用户输入的日期不符合格式规范时,可以提示用户重新输入,而不是抛出异常。

祝学习愉快~

  • K7G 提问者 #1

    已修改请老师查看

    package com.imooc.jdbc.goods;
    /**
     * 写入类
     */
    
    import com.imooc.common.DbUtils;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Scanner;
    
    public class Insert implements Command {
    
        @Override
        public void execute() {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入商品名称:");
            String name = sc.next();
            System.out.println("请输入商品价格:");
            Float price = sc.nextFloat();
            System.out.println("请输入商品描述:");
            String desp = sc.next();
            boolean i=true;
            while (i) {
                    System.out.println("请输入日期:(格式为yyyy-mm-dd)");
                    java.util.Date udCreate_time = null;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    String create_time = sc.next();
                    udCreate_time = sdf.parse(create_time);
                } catch (Exception e) {
                    System.out.println("输入错误,请按照输入格式");
                    continue;
                }
                i=false;
                long time = udCreate_time.getTime();
                java.sql.Date sdCreate_time = new java.sql.Date(time);
                Connection conn = null;
                PreparedStatement pstmt = null;
                try {
                    conn = DbUtils.getConnection();
                    String sql = "insert into goods(name,price,desp,create_time) values(?,?,?,?)";
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1, name);
                    pstmt.setFloat(2, price);
                    pstmt.setString(3, desp);
                    pstmt.setDate(4, sdCreate_time);
                    int cnt = pstmt.executeUpdate();
                    if (cnt == 1) {
                        System.out.println("成功添加" + cnt + "条");
                    } else {
                        System.out.println("添加失败");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    DbUtils.closeConnection(null, pstmt, conn);
                }
                break;
            }
        }
    }

    https://img1.sycdn.imooc.com//climg/63ff1e0609fc1c1105310203.jpg

    https://img1.sycdn.imooc.com//climg/63ff1e060908dee107360477.jpg


    改的时候有一个问题:

    catch下加了sc.next()来捕获时,会出现多输入一次,需要改成什么比较好?

    https://img1.sycdn.imooc.com//climg/63ff1f9209de76ae09460196.jpghttps://img1.sycdn.imooc.com//climg/63ff1f9209f36f3c05420543.jpg


    2023-03-01 17:55:49
  • 好帮手慕小蓝 回复 提问者 K7G #2

    同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

    代码中不需要使用next方法,因为异常抛出的原因是在格式化时出现的,而不是Scanner接收到了不合法数据。

    祝学习愉快~

    2023-03-01 18:02:37
  • K7G 提问者 回复 好帮手慕小蓝 #3

    明白了,谢谢老师~

    2023-03-01 18:05:58
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师