gpt4 book ai didi

java - MySQL classNotFoundException & 找不到适合 jdbc 的驱动程序

转载 作者:行者123 更新时间:2023-11-29 21:04:43 25 4
gpt4 key购买 nike

我是刚开始学习JSP。我添加了这个jar文件mysql-connector-java-5.1.38-bin.jar mysql-connector

IML 文件:

<CLASSES>
<root url="jar://$USER_HOME$/Downloads/mysql-connector-java-5.1.38/mysql-connector-java-5.1.38/mysql-connector-java-5.1.38-bin.jar!/" />
</CLASSES>

我做了一个这样的表类:

package test;
public class Student {
private int id;
private String name;

public int getId(){
return id;
}

public void setId(int id){
this.id = id;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public Student(int id,String name){
super();
this.name = name;
this.id = id;
}
}

和一个操作类:

package test;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentOperation {
public List readStudent(){
List<Student> list = new ArrayList<>();
com.mysql.jdbc.Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

try{
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
try{
connection = (com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root",null);
String sql = "SELECT * FROM student WHERE student_id>=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,1);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
String studentName = resultSet.getString("student_name");
int studentId = resultSet.getInt("student_id");
Student student = new Student(studentId,studentName);
list.add(student);
}
}catch (SQLException e){
e.printStackTrace();
}finally {
try{
if(resultSet!=null){
resultSet.close();
}
if(preparedStatement!=null){
preparedStatement.close();
}
if(connection!=null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
return list;
}
public static void main(String[] args){
StudentOperation studentOperation = new StudentOperation();
List<Student> list = studentOperation.readStudent();
System.out.println(list.size());
for(Student student : list){
System.out.println(student.getName()+"\t");
System.out.println(student.getId());
}
}
}

运行main方法,输出结果如下:

3
student3
201592385
student2
201592386
student1
201592387

这是正确的结果。但是当我添加到jsp文件时:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="test.Student" %>
<%@ page import="test.StudentOperation" %>
<%@ page import="java.util.List" %>
<html>
<head>
<title>Homework</title>
</head>
<body>
<h1 align="center">Student Database</h1>

<table border="1">
<tr>
<td>Student Name</td>
<td>Student ID</td>
</tr>
<%
try {
StudentOperation studentOperation = new StudentOperation();
List<Student> list = studentOperation.readStudent();
for(Student student : list){ %>
<tr>
<td><%= student.getName() %></td>
<td><%= student.getId() %></td>
</tr>
<% }
}catch (Exception e){e.printStackTrace();}
%>
</table>
</body>
</html>

是这样的: the result并抛出异常:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/student

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

出了什么问题?

最佳答案

将驱动jar放入服务器lib文件夹

即在 ($CATALINA_HOME/lib) 然后检查。希望对您有帮助

关于java - MySQL classNotFoundException & 找不到适合 jdbc 的驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com