JavaWeb-jdbc的mysql驱动问题

jdbc的mysql驱动问题

问题描述:mysql驱动在服务器运行时显示找不到类,在程序运行时正常。

解决办法:主要是驱动没有放对目录,将mysql驱动放到WEB-INF/lib目录下就能正常访问了

添加为库

报错信息

java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

示例代码

程序
java 复制代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcExample {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        // 数据库连接信息
        String url = "jdbc:mysql://ip:端口/数据库名?useSSL=false";
        String username = "用户名";
        String password = "密码";

        // JDBC对象
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接数据库
            connection = DriverManager.getConnection(url, username, password);

            // 创建Statement对象
            statement = connection.createStatement();

            // 执行查询语句
            String query = "SELECT * FROM users";
            resultSet = statement.executeQuery(query);

            // 处理查询结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("age");

                System.out.println("ID: " + id);
                System.out.println("Name: " + name);
                System.out.println("age: " + email);
                System.out.println("----------------------");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
服务器
java 复制代码
package com.chuwu.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/dome2")
public class ServletDemo2 extends HttpServlet {
    private void doMysql() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        // 数据库连接信息
        String url = "jdbc:mysql://ip:端口/数据库?useSSL=false";
        String username = "用户名";
        String password = "密码";

        // JDBC对象
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接数据库
            connection = DriverManager.getConnection(url, username, password);

            // 创建Statement对象
            statement = connection.createStatement();

            // 执行查询语句
            String query = "SELECT * FROM users";
            resultSet = statement.executeQuery(query);

            // 处理查询结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("age");

                System.out.println("ID: " + id);
                System.out.println("Name: " + name);
                System.out.println("age: " + email);
                System.out.println("----------------------");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get请求");
        try {
            doMysql();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post请求");
    }
}
相关推荐
web安全工具库32 分钟前
Makefile 模式规则精讲:从 %.o: %.c 到静态模式规则的终极自动化
linux·运维·c语言·开发语言·数据库·自动化
'需尽欢'32 分钟前
基于 Flask+Vue+MySQL的研学网站
python·mysql·flask
清静诗意2 小时前
Ubuntu 系统下 MySQL 8.0 安装与远程访问完整教程
mysql·ubuntu·adb
盒马coding8 小时前
第19节-非规范化数据类型-Composite-types
数据库·postgresql
-雷阵雨-8 小时前
MySQL——桥梁JDBC
数据库·mysql·oracle
亿坊电商8 小时前
在PHP框架里如何进行数据库连接?
数据库·oracle·php
满昕欢喜8 小时前
SQL Server从入门到项目实践(超值版)读书笔记 28
数据库·sql·sqlserver
楚韵天工9 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
JanelSirry10 小时前
MySQL分区表(PARTITION):水平分表示例 (基于用户ID哈希分表)不依赖第三方中间件
mysql·中间件·哈希算法
李白你好10 小时前
一款专业的多数据库安全评估工具,支持 **PostgreSQL、MySQL、Redis、MSSQL** 等多种数据库的后渗透操作
数据库·mysql·postgresql