为什么我会得到“找不到适用于jdbc:mysql://localhost:3306/test2的合适驱动程序"? [英] Why am I getting "No suitable driver found for jdbc:mysql://localhost:3306/test2"?
问题描述
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test2
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.room302.util.DBConnectionUtil.getConnection(DBConnectionUtil.java:32)
at com.room302.dao.roomDAO.addexpense(roomDAO.java:26)
at com.room302.servlet.Navigator.doPost(Navigator.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的Util文件如下:
My Util file is as follows:
public class DBConnectionUtil
{
private static final String USERNAME="root";
private static final String PASSWORD="1234";
private static final String URL="jdbc:mysql://localhost:3306/test2";
private static final String DRIVER_NAME="mysql";
private static Connection conn=null;
static
{
try
{
System.out.println("searching for driver");
Class.forName(DRIVER_NAME);
}
catch(ClassNotFoundException cnfe)
{
}
}
public static Connection getConnection() throws SQLException
{
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeConnection(Connection conn)
{
if(conn!=null)
{
try
{
conn.close();
}
catch (Exception e)
{
}
}
}
}
我已经将MySQL连接器放在了buildpath以及web-inf的lib文件夹中. 为什么我仍然收到此错误?
I have put MySQL connector in the buildpath and also in lib folder of web-inf. Why am I still getting this error?
推荐答案
您需要加载com.mysql.jdbc.Driver
驱动程序类.
You need to load the com.mysql.jdbc.Driver
driver class.
private static final String DRIVER_NAME="com.mysql.jdbc.Driver";
查看官方文档:
在MySQL Connector/J中实现java.sql.Driver的类的名称为com.mysql.jdbc.Driver. org.gjt.mm.mysql.Driver类名称也可用于与MM.MySQL(Connector/J的前身)向后兼容.注册驱动程序或以其他方式配置软件以使用MySQL Connector/J时,请使用此类名称.
The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver. The org.gjt.mm.mysql.Driver class name is also usable for backward compatibility with MM.MySQL, the predecessor of Connector/J. Use this class name when registering the driver, or when otherwise configuring software to use MySQL Connector/J.
这篇关于为什么我会得到“找不到适用于jdbc:mysql://localhost:3306/test2的合适驱动程序"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!