java.sql.SQLException:找不到适合 jdbc 的驱动程序:mysql://localhost:3306/grey_goose
我知道,当我们没有在classPath中包含JAR文件时,就会发生此错误,但是我已经这样做了,并且我的JDBC程序在同一项目中运行完美,但是当我尝试将JDBC与Servlet一起使用时,它给了我这个错误。
java.sql.sqlexception:没有找到适合JDBC的驱动程序:mysql:// localhost:3306/grey_goose
我尝试了堆栈溢出中的所有解决方案,但问题仍然存在。
我也试图添加
class.forname(“ com.mysql.cj.jdbc.driver”);
但这给了我另一个错误
java.lang.noclassdeffounderror:java/sql/driver
这是我的servlet
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/grey_goose";
String uname="root";
String pass="Karmveer@2001";
String query="SELECT * FROM customer limit 3";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
try {
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
out.println("<table border=1 width=50% height=50%>");
out.println("<tr><th>cust_number</th><th>name_customer</th><th>");
while(rs.next()) {
String n = rs.getString("cust_number");
String nm = rs.getString("name_customer");
out.println("<tr><td>" + n + "</td><td>" + nm + "</td></tr>");
}
out.println("</table>");
out.println("</html></body>");
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.akaaliStudios.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet</url-pattern>
</servlet-mapping>
</web-app>
一个单独的jdbc程序在同一项目中运行良好,这意味着jar文件正确加载了,但是当我尝试使用时,问题就来了JDBC与servlets。
- 我已经在tomcat安装文件夹连接器jar文件的LIB文件夹中添加了连接器
- jar文件,在项目属性中的Web部署组件中添加了。
- 连接器jar文件添加到项目属性中的Java构建路径的类路径中。
- 连接器jar文件添加到启动配置Tomcat服务器的引导程序条目中。
I know that this error occurs when we have not included JAR file in the classpath, but I have already done that and my JDBC program in the same project runs perfect, but when I am trying to use JDBC with servlet, it gives me this error.
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/grey_goose
I have tried all the solutions in Stack Overflow, but the problem persists.
I also tried to add
Class.forName("com.mysql.cj.jdbc.Driver");
but it gives me another error
java.lang.NoClassDefFoundError: java/sql/Driver
This is my servlet
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/grey_goose";
String uname="root";
String pass="Karmveer@2001";
String query="SELECT * FROM customer limit 3";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
try {
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
out.println("<table border=1 width=50% height=50%>");
out.println("<tr><th>cust_number</th><th>name_customer</th><th>");
while(rs.next()) {
String n = rs.getString("cust_number");
String nm = rs.getString("name_customer");
out.println("<tr><td>" + n + "</td><td>" + nm + "</td></tr>");
}
out.println("</table>");
out.println("</html></body>");
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.akaaliStudios.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet</url-pattern>
</servlet-mapping>
</web-app>
A separate JDBC program is running fine in the same project that means that JAR files are loaded correctly, but the problem comes when I try to use JDBC with servlets.
- I have added connector JAR file in lib folder of Tomcat installation folder
- connector JAR file is added in web deployment assembly in project properties.
- connector JAR file is added in class path of Java build path in project properties.
- connector JAR file is added in bootstrap entries of launch configuration Tomcat server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论