我有驱动程序,class.forname()不会抛出异常,但"没有合适的MySQL驱动程序"

Asc*_*ant 1 java mysql jdbc

我知道这个问题突然出现但我需要问这个问题.

我有包含mysql jdbc驱动程序的jar文件,即mysql-connector-java-5.1.12-bin.jar.
我打开了这个jar文件并确认这个jar文件确实包含com.mysql.jdbc.Connection.class.

Class.forName("com.mysql.jdbc.Connection");
Run Code Online (Sandbox Code Playgroud)

这不会引发任何异常.

当我尝试与下面的代码建立连接时,我得到一个例外.

con = DriverManager.getConnection(dbURL,dbUNM,dbUPW);
Run Code Online (Sandbox Code Playgroud)

(我不确定将连接URL放在哪里是明智的)

我明白了

java.sql.SQLException: No suitable driver found for jdbc:mysql:xxx....
Run Code Online (Sandbox Code Playgroud)

我也明白了

    Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
    at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:624)
    at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:823)
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:1134)
Run Code Online (Sandbox Code Playgroud)

你能指出我做错了什么吗?谢谢 : )

Sco*_*ion 6

您需要使用Class.forName加载驱动程序类 - 尝试以下代码行

Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)