xxgirl2
V2EX  ›  Java

java 不能加载 mysql 驱动……

  •  
  •   xxgirl2 · Jul 6, 2015 · 6448 views
    This topic created in 4139 days ago, the information mentioned may be changed or developed.

    代码 Class.forName("com.mysql.jdbc.Driver"); 永远都是 ClassNotFoundException

    尝试过以下方案,同时尝试过,也分别尝试过:
    一、在Build Path里加入mysqlconn.jar
    二、在Build Path里加入包含这个jar的目录
    三、在Tomcat的lib里复制进这个jar
    四、在WEB-INF/lib里复制进这个jar
    五、调整、勾选library的顺序
    以上方案均无效。

    重装3件套无效,以管理员权限运行无效。
    总之全部无效。永远都是 not found。

    另外,代码 Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "toor"); 给出的是 SQLException
    尝试过代码 com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); 给出的也是
    SQLException

    不加载jdbc的话,其余代码正常,也能正常在浏览器中显示。

    这到底是怎么回事,该如何解决?

    环境:
    Eclipse Java EE - Mars Release (4.5.0) Build 20150621-1200
    JDK 7u75
    Tomcat 8.0.23
    mysql-connector-java-5.1.35-bin.jar
    Windows 6.3.9600

    24 replies  •  2015-07-15 23:13:56 +08:00
    xuhaoyangx
    xuhaoyangx
    Jul 6, 2015
    1
    private String sqlServerDriver="com.mysql.jdbc.Driver";
    private String sqlUrl="jdbc:mysql://localhost/xxx";
    private String sqlUser="root";
    private String sqlPassword="";
    Class.forName(sqlServerDriver);
    ct=DriverManager.getConnection(sqlUrl, sqlUser, sqlPassword);
    funky
    funky
    Jul 6, 2015
    2
    jar加入build path之后去看jar下是不是有这个类不就完了么,
    顺便吐槽下楼上的变量命名 sqlServerDriver 不应该是 mysqlDriver么
    xxgirl2
    xxgirl2
    OP
    Jul 6, 2015
    3
    @xuhaoyangx 真不是语法问题,这种格式也挂了。

    @funky 当然有。那个包都解开看过了。这玩意就在源码的眼皮子底下它也不识别
    funky
    funky
    Jul 6, 2015
    4
    @xxgirl2 jdbc4.0规范之后不需要显示去加载驱动,方便的话私信我funkyyj#gmail.com
    funky
    funky
    Jul 6, 2015
    5
    显示==>显式
    xxgirl2
    xxgirl2
    OP
    Jul 6, 2015
    6
    @funky DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 中
    new com.mysql.jdbc.Driver() 又是一个 SQLException……
    直接 getConnection 也是 SQLException
    xuhaoyangx
    xuhaoyangx
    Jul 6, 2015
    7
    @funky 不要在意细节。。。
    Ouyangan
    Ouyangan
    Jul 6, 2015
    8
    把完整log贴出来
    xxgirl2
    xxgirl2
    OP
    Jul 6, 2015
    9
    @Ouyangan


    七月 06, 2015 5:01:11 下午 org.apache.catalina.core.StandardWrapperValve invoke
    严重: Servlet.service() for servlet [D] in context with path [/w] threw exception [Servlet execution threw an exception] with root cause
    java.lang.Error: Unresolved compilation problem:
    Unhandled exception type ClassNotFoundException

    at D.doGet(D.java:33)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
    coetzee
    coetzee
    Jul 6, 2015
    10
    @xxgirl2 编译就报错了,看一下jar包的加载顺序吧
    SevilinMa
    SevilinMa
    Jul 6, 2015
    11
    建议你采用JNDI的方式加载JDBC或者使用pool吧
    incompatible
    incompatible
    Jul 6, 2015
    12
    有没有试过在build path的Libraries这里加入这个jar包?
    xxgirl2
    xxgirl2
    OP
    Jul 6, 2015
    13
    @incompatible 试过,无效。

    @SevilinMa new InitialContext() -> NamingException。打死也不认"com.mysql.jdbc.Driver"

    @coetzee 还望明示
    wintersun
    wintersun
    Jul 6, 2015
    14
    代码发我,调好发回给你,wintersun_ph#qq.com,不谢!
    wintersun
    wintersun
    Jul 6, 2015
    15
    楼主的代码我改动后成功运行了,加载驱动无问题!
    已经把答案发给楼主了,结贴散分!
    broadliyn
    broadliyn
    Jul 6, 2015
    16
    我猜是classpath没有设置好
    zonghua
    zonghua
    Jul 7, 2015 via iPhone
    17
    你这个是servlet环境都还没有吧
    xxgirl2
    xxgirl2
    OP
    Jul 7, 2015
    18
    @wintersun 好吧,纠结了近20个小时的问题被 try-catch 解决了,真的感谢。
    于是这玩意为啥需要包一层 try-catch,这东西难道不是只有出问题的时候才访问吗?
    刚刚接触 java 实在想不到这一点上,不知哪里有文档可以学习这块。
    msg7086
    msg7086
    Jul 7, 2015
    19
    @xxgirl2 Java的世界就是无尽地与编译器做斗争的世界。
    wintersun
    wintersun
    Jul 7, 2015
    20
    @xxgirl2
    论保险跟try...catch

    [两者的共性]
    你都不希望意外发生,但它有可能发生(小概率事件),你必须为此做出准备——买保险或是写try...catch

    [保险的作用]
    当你出现意外、重大疾病时赔付给你,解你燃眉之急的!

    [两者的运作]
    前提是你得先买保险——代码里针对可能出状况的部分写入try...catch
    发生情况时,保险赔付生效,帮助你面对困难,减少你的损失——try...catch代码执行,保证程序不因为异常而崩溃,遇到致命error也能优雅退出

    [其他语言]
    try-catch者,java有之,c#有之,c++有之(这三家可以说共一个祖宗来的),新出的swift也有之。

    [问题的本质]
    导入楼主代码到Eclipse,硕大的编译错误伫立在那里——强类型、编译型的Java,Eclipse已经对其进行了各种周到的validation、语法检查,楼主只要跟着Eclipse走就好了;实际上你用命令行编译也会有这个报错!
    Neveroldmilk
    Neveroldmilk
    Jul 7, 2015
    21
    只是加了try...catch就解决了?这么神奇?
    chocotan
    chocotan
    Jul 7, 2015
    22
    用了ide为啥还出现这样的情况 楼主无视红色的error了吗
    thinkmore
    thinkmore
    Jul 7, 2015
    23
    你是不是没有安装mysql呀,其次换一个驱动包试一试
    jy04149886
    jy04149886
    Jul 15, 2015
    24
    直接扔到tomcat的lib下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1240 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 66ms · UTC 22:17 · PVG 06:17 · LAX 14:17 · JFK 17:17
    ♥ Do have faith in what you're doing.