简体   繁体   中英

MySQL classNotFoundException & no suitable Driver found for jdbc

I'm new to study JSP. I have added this jar file mysql-connector-java-5.1.38-bin.jar mysql-connector

The IML file:

<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>

I made a class of a table like this:

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;
  }
}

and a operating class:

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());
    }
}
}

run the main method, it output the result like this:

3
student3    
201592385
student2    
201592386
student1    
201592387

It's the right result. But when I add to the jsp file:

<%@ 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>

It is like this: the result And throw the exception:

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

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

What's wrong?

put the jar of driver in the server lib folder

ie at ($CATALINA_HOME/lib) Then check. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM