【问题标题】:MySQL classNotFoundException & no suitable Driver found for jdbcMySQL classNotFoundException & 找不到适合 jdbc 的驱动程序
【发布时间】:2016-04-28 11:40:32
【问题描述】:

我是学习 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

怎么了?

【问题讨论】:

  • 那么,您是否尝试将“java.sql.SQLException:未找到合适的驱动程序”转储到您最喜欢的搜索引擎中,并阅读了数十个关于完全相同问题的现有 stackoverflow 问题?

标签: java mysql jsp jdbc


【解决方案1】:

把驱动的jar放到服务器的lib文件夹中

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

【讨论】:

  • 有效!非常感谢。你能告诉我是什么导致了这个问题吗?
  • 因为在应用实例化之前,必须先建立连接池。
猜你喜欢
  • 2016-04-02
  • 2012-07-14
  • 1970-01-01
  • 2012-01-15
  • 2020-07-09
  • 2016-09-07
  • 1970-01-01
  • 2011-09-10
相关资源
最近更新 更多