qq_33759198 2017-07-06 09:37 采纳率: 100%
浏览 1022
已采纳

读properties class.forname失败

Java 连接Mysql 加载 直接写Class.forName("com.mysql.jdbc.Driver"); 可以成功,但读properties Class.forName(drivers);); drivers = props.getProperty("driver“”( System.out.println(drivers)也可打印com.mysql.jdbc.Driver成功证明取到了 "); 就失败报java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 读url user passw读到可打印 但就是报错都是 具体代码如下ord
private void init() {

try {

// InputStream in = getClass().getResourceAsStream(FILE_PATH_NAME);

InputStream in = DataConnection.class.getClassLoader().getResourceAsStream(FILE_PATH_NAME);

Properties props = new Properties();

     props.load(in);    
     in.close();    
     drivers = props.getProperty("driver");    
     url = props.getProperty("url");    
     username = props.getProperty("username");    
     password = props.getProperty("password");    
 } catch (IOException e) {    
     e.printStackTrace();    
 }    

}

public Connection getConnection() {

Connection conn = null;

try {

System.out.println(drivers);
Class.forName(drivers);
// System.out.println("com.mysql.jdbc.Driver");
// Class.forName("com.mysql.jdbc.Driver");

    // conn = DriverManager.getConnection(url, username, password); 
     conn = DriverManager.getConnection("jdbc:mysql://10.5.0.20/robottest","root", "1234");
 } catch (SQLException e) {    
     e.printStackTrace();    
 } catch (ClassNotFoundException e) {    
     e.printStackTrace();    
 }    
 return conn;    

}

报错

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at dao.DataConnection.getConnection(DataConnection.java:53)
at org.apache.jsp.connTest_jsp._jspService(connTest_jsp.java:120)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.atguigu.javaweb.AuthorityFilter.doFilter(AuthorityFilter.java:29)
at com.atguigu.javaweb.HttpFilter.doFilter(HttpFilter.java:59)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)

  • 写回答

2条回答 默认 最新

  • jinshi366 2017-07-07 02:37
    关注

    Class.forName("\""+drivers+"\""); 这样试试?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题