I put mysql-connector-java-8.0.30.jar
in WEB-INF/lib/
Tomcat thows:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/school
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at cn.korilweb.dao.StudentDao.selectAllStudents(StudentDao.java:27)
at cn.korilweb.servlet.StudentServlet.doGet(StudentServlet.java:23)
But it can work when I put mysql-connector-java-8.0.30.jar
in D:\apache-tomcat-8.5.61\lib\
Environment Information:
- My Computer System: Windows 10
- Java Version: java 11.0.16.1
- Tomcat Path: D:\apache-tomcat-8.5.61\
- MySQL Version: mysql Ver 8.0.22 for Win64 on x86_64
- MySQL Driver Version: mysql-connector-java-8.0.30.jar
I started MySQL on my local machine, username is root, passward is 123456.
I wrote a simple servlet--StudentServlet
, the StudentServlet
used StudentDao
to retrieve student's data from MySQL.
Code:
StudentServlet.java
package cn.korilweb.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import cn.korilweb.dao.StudentDao;
import cn.korilweb.entity.Student;
import java.util.List;
public class StudentServlet extends HttpServlet {
private StudentDao dao = new StudentDao();
public void doGet(HttpServletRequest req, HttpServletResponse res) {
try {
PrintWriter out = res.getWriter();
List<Student> students = dao.selectAllStudents();
for (Student s : students) {
out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
StudentDao.java
package cn.korilweb.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
import cn.korilweb.entity.Student;
public class StudentDao {
final String JDBC_URL = "jdbc:mysql://localhost:3306/school";
final String USER = "root";
final String PASSWARD = "123456";
public List<Student> selectAllStudents() throws Exception {
List<Student> students = new ArrayList<>();
String sql = "SELECT * FROM student";
System.out.println("sql: " + sql);
try (
Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASSWARD);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql)
) {
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
students.add(s);
}
}
return students;
}
public static void main(String[] args) throws Exception {
StudentDao dao = new StudentDao();
List<Student> students = dao.selectAllStudents();
for (Student s : students) {
System.out.println(s);
}
}
}
Student.java
package cn.korilweb.entity;
public class Student {
private int id;
private String name;
private int age;
// Some getter, setter, toString method
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>cn.korilweb.servlet.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/students</url-pattern>
</servlet-mapping>
</web-app>
My trial
I wrote a main
method in StudentDao
to test, and it works!
here is my cmd result:
> javac -cp .;..\lib\mysql-connector-java-8.0.30.jar cn\korilweb\dao\StudentDao.java
> java -cp .;..\lib\mysql-connector-java-8.0.30.jar cn.korilweb.dao.StudentDao
sql: SELECT * FROM student
Student [id=1, name=djh, age=23]
-----------
sql: INSERT INTO student VALUES (2, "lzq", 24)
After that,I put these classes in D:\apache-tomcat-8.5.61\webapps\student-app\
, here is my directory structure:
D:\apache-tomcat-8.5.61\webapps
└─student-app
└─WEB-INF
├─classes
│ └─cn
│ └─korilweb
│ ├─dao
│ │ └─StudentDao.class
│ │
│ ├─entity
│ │ └─Student.class
│ │
│ └─servlet
│ └─StudentServlet.class
│
├─lib
│ ├─javax.servlet-api-3.1.0.jar
│ │
│ └─mysql-connector-java-8.0.30.jar
│
└─web.xml
I run D:\apache-tomcat-8.5.61\bin\startup.bat
to start Tomcat Server on my local machine, and inputhttp://127.0.0.1:8080/student-app/students
in browser, then I got the Exception Message:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/school
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at cn.korilweb.dao.StudentDao.selectAllStudents(StudentDao.java:27)
...
My Problem
After google this problem, I try to put mysql-connector-java-8.0.30.jar
in D:\apache-tomcat-8.5.61\lib\
, and I found it can work, but why? I can't understand, please help me.
I intentionally not use IDEA, Maven to help me build first simple web project because I want to know the steps of manual operation without advanced tools.