2016-04-28 147 views
0

我是學習JSP的新手。 我加入這個jar文件mysql-connector-java-5.1.38-bin.jar mysql-connectorMySQL classNotFoundException&找不到適用於jdbc的驅動程序

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

運行主要方法如下:

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 

有什麼不對?

+0

因此,您是否嘗試將「java.sql.SQLException:找不到合適的驅動程序」轉儲到您最喜愛的搜索引擎中,並通過幾十個關於完全相同問題的現有stackoverflow問題進行了解讀? – Gimby

回答

0

把司機的罐子在服務器lib文件夾

即在($ CATALINA_HOME/lib目錄),然後檢查。希望它有幫助

+0

它的工作原理!非常感謝。你能告訴我是什麼原因導致了這個問題? – WOC666

+0

因爲在應用程序被實例化之前,必須設置連接池。 –

相關問題