我是研究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
怎么了?
答案 0 :(得分:0)
将jar驱动程序放在服务器lib文件夹中
即。 at($ CATALINA_HOME / lib)然后检查。希望它有所帮助