JDBC入门 作业4-4 运行异常问题

JDBC入门 作业4-4 运行异常问题

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,'滚筒');
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.InsertCommand;

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;

        }
    }
}
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);
        }
    }
}

运行截图

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

正在回答

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

1回答

同学你好,报错信息中显式找不到驱动类,建议同学检查一下项目中是否引入了mysql的驱动jar包。

老师这里使用引入过驱动的项目环境,是可以正常运行的。

祝学习愉快~

  • rudtjd 提问者 #1

    好的好的,一直是导入的,但是没有add as Library :( 谢谢老师

    2023-02-08 13:43:49
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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