BUZHIDAO52 2022-12-11 10:53 采纳率: 40%
浏览 66
已结题

数据库连接报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的问题

**今天看了一下Servlet的知识,了解了数据如何从客户端传到服务器,我就想用JDBC的知识,把从客户端获得的数据经过服务器处理,操控数据表,但是在请求数据库连接的时候出现了问题
**

数据库连接代码:

public static Connection getConnection() throws Exception {
        InputStream is = JDBCutil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driver = pros.getProperty("driver");

        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        return  connection;
    }

服务器处理数据代码:

public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fname = req.getParameter("fname");

        String priceStr = req.getParameter("price");
        Integer price = Integer.parseInt(priceStr);

        String fcountStr = req.getParameter("fcount");
        Integer fcount = Integer.parseInt(fcountStr);

        String remark = req.getParameter("remark");

        System.out.println(fname);
        System.out.println(price);
        System.out.println(fcount);
        System.out.println(remark);


        FruitDAOImpl fruitDAO = new FruitDAOImpl();
        Connection connection = null;
        try {
            connection = JDBCutil.getConnection();
            Fruit fruit = new Fruit(1,fname,price,fcount,remark);
            fruitDAO.insert(connection,fruit);
            System.out.println("insert success");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCutil.closeResource(connection,null);
        }
    }



前端是一个表单输入数据
服务器可以获取到数据,但是报错,报错位置在我连接数据库的代码上

img

是这句

//2.加载driver
        Class.forName(driver);

但是!我在自己另外写的测试数据库连接的代码中,运行确实成功的,烦请各位指正


@Test
    public void testConnection05() throws Exception {
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driver = pros.getProperty("driver");

        //2.加载驱动
        Class.forName(driver);

        //3.获取连接
        Connection connection = (Connection) DriverManager.getConnection(url,user,password);
        System.out.println(connection);

    }

img

下面是我导入的jar包和我的配置文件

img

user=root
password=123456
url=jdbc:mysql://localhost:3306/travel?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&serverTimezone=UTC
driver=com.mysql.jdbc.Driver

img

提前谢谢各位!

  • 写回答

4条回答 默认 最新

  • BUZHIDAO52 2022-12-13 06:10
    关注

    已经解决了各位,我重新配了jar包的路径,再次运行就可以了,应该是我jar包导入的有问题,谢谢各位的提议

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 12月11日

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存