抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [英] java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

查看:128
本文介绍了抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行代码时,我收到异常 java.lang.ClassNotFoundException > 我的代码

  try 
{
Class.forName(com .mysql.jdbc.Driver);
连接con = DriverManager.getConnection(jdbc:mysql:// localhost:3306 / simple,
root,root);
语句stmt = con.createStatement();
String query =SELECT * FROM CUST;
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
System.out.print(rs.getString(CUST_NAME)+);
System.out.print(rs.getString(2)+);
System.out.print(rs.getString(3)+);

}
rs.close();
stmt.close();
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}

我收到错误

  java.lang.ClassNotFoundException:com.mysql.jdbc.Driver 
在java.net.URLClassLoader $ 1.run(URLClassLoader.java :200)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass(URLClassLoader.java:188)
在java.lang.ClassLoader.loadClass (ClassLoader.java:307)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)
在java.lang.ClassLoader.loadClass(ClassLoader.java:252)
在java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
在java.lang.Class.forName0(本机方法)
在java.lang.Class.forName(Class.java:169 )
在Simple.MyProg.main(MyProg.java:15)

我是什么做错了?

解决方案

问题不在代码中,但您没有将驱动程序添加到您的项目中!
您必须将 *。jar 驱动程序添加到您的项目...



尝试将此在您的lib目录中,然后重新启动tomcat ...



问题是 Class.forName(com.mysql.jdbc.Driver) ;
它尝试加载驱动程序,但它没有得到它,这是你获得java.lang.ClassNotFoundException的原因。


I'm getting the exception java.lang.ClassNotFoundException when I am trying to run my code,

My Code

   try 
   {
     Class.forName("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/simple",
      "root","root");
     Statement stmt=con.createStatement();
     String query="SELECT * FROM CUST";
     ResultSet rs=stmt.executeQuery(query);
     while(rs.next())
     {
          System.out.print(rs.getString("CUST_NAME") +" ");
          System.out.print(rs.getString(2) +" ");
          System.out.print(rs.getString(3) +" ");

     }    
     rs.close();
     stmt.close();
     con.close();    
  }   
  catch (ClassNotFoundException e) 
  {
     e.printStackTrace();
  }
  catch (SQLException e) 
  {
     e.printStackTrace();
  }    

I'm getting Error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Simple.MyProg.main(MyProg.java:15)   

What am I doing wrong?

解决方案

problem is not in the code, but you don't have added the driver to your project!!! You have to add the *.jar driver to your project...

Try putting this in your lib directory, then re-starting tomcat...

problem is Class.forName("com.mysql.jdbc.Driver"); it tries to load the driver, but it is not getting it, this is the reason you are getting java.lang.ClassNotFoundException.

这篇关于抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆