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请求");
}
}