JDBC入门 作业4-4

JDBC入门 作业4-4

package com.imooc.jdbc.common;

import java.sql.*;

public class DbUtils {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/goods?useSSL=false&useUnicode=true&usecharacterEnconding=utf-8&serverTimezone=Asia/Shanghai&allPublicKeyRetrieval=true",
                "root", "root");
        return conn;
    }

    public static void closeConnection(ResultSet rs, Statement stmt, Connection conn) {
        try {
            //5. 关闭连接,释放资源
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
package com.imooc.jdbc;
import com.imooc.jdbc.command.Command;
import com.imooc.jdbc.command.DeleteCommand;
import com.imooc.jdbc.command.InsertCommand;
import com.imooc.jdbc.command.UpdateCommand;

import java.util.Scanner;

public class GoodsRescource {
    public static void main(String[] args) {
        System.out.println("1-添加新的商品");
        System.out.println("2-更新商品信息");
        System.out.println("3-删除商品信息");
        System.out.println("请选择功能:");
        Scanner in = new Scanner(System.in);
        Integer cmd = in.nextInt();

        Command command = null;
        switch (cmd){
            case 1:     //查询部门员工
                command = new InsertCommand();
                command.execute();  //执行
                break;
            case 2:
                command = new UpdateCommand();
                command.execute();
                break;
            case 3:
                command = new DeleteCommand();
                command.execute();
                break;
        }
    }
}
package com.imooc.jdbc.command;

import com.imooc.jdbc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class InsertCommand implements Command {
    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入商品名字:");
        String name = in.next();
        System.out.println("请输入商品价格:");
        float price = in.nextFloat();
        System.out.println("请输入商品描述:");
        String desp = in.next();

        Connection conn=null;
        PreparedStatement pstmt = null;

        try {
            conn = DbUtils.getConnection();
            String sql = " insert into  goods(name,price,desp) values (?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setFloat(2,price);
            pstmt.setString(3,desp);
            int cnt = pstmt.executeUpdate();
            System.out.println(cnt+"商品信息已添加。");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtils.closeConnection(null,pstmt,conn);
        }
    }
}
package com.imooc.jdbc.command;

import com.imooc.jdbc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Scanner;
import java.util.concurrent.ForkJoinPool;

public class UpdateCommand implements Command{
    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入商品编号:");
        int id = in.nextInt();
        System.out.println("请输入商品新的价格:");
        Float price = in.nextFloat();

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DbUtils.getConnection();
            String sql = " Update goods set price=? where id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setFloat(1,price);
            pstmt.setInt(2,id);
            int cnt = pstmt.executeUpdate();
            if ( cnt==1 ){
                System.out.println("已成功更改商品价格。");
            }else{
                System.out.println("未找到"+id+"编号的商品。");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtils.closeConnection(null,pstmt,conn);
        }

    }
}
package com.imooc.jdbc.command;

import com.imooc.jdbc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class DeleteCommand implements Command{
    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入商品编号:");
        int id = in.nextInt();

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DbUtils.getConnection();
            String sql = " Delete from goods where id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            int cnt = pstmt.executeUpdate();
            if ( cnt==1 ){
                System.out.println("已成功删除商品信息。");
            }else{
                System.out.println("删除失败,未找到"+id+"编号的商品。");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtils.closeConnection(null,pstmt,conn);
        }
    }
}
create table goods(
id int primary key auto_increment COMMENT "商品编号",
name varchar(20) not null COMMENT "商品名称",
price float not null comment"商品价格",
desp varchar(20) not null comment"商品描述");
Insert into goods(name,price,desp) 
values ('手机',2000,'黑色,存储容量'),
('冰箱',1500,'银色,对开门'),
('洗衣机',3000,'滚筒');


运行截图:

https://img1.sycdn.imooc.com//climg/63e33ac709c8b2cf06260413.jpghttps://img1.sycdn.imooc.com//climg/63e33ad209dd295f04390201.jpg

https://img1.sycdn.imooc.com//climg/63e33aea090c06a606180353.jpghttps://img1.sycdn.imooc.com//climg/63e33aef0909600904260181.jpg

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

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


前面发的两个问题老师可以帮忙删一下惹 略微刷屏 谢谢老师 

正在回答

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

1回答

同学你好,已完成练习代码实现符合题目要求,逻辑清晰继续加油!另老师没有删除权限,无法删除同学的问答,若日后遇到类似问题也可将其做笔记查看回顾的。

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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