首页 新闻 会员 周边

java web 中加载mySql数据库出现ClassNotFoundException

0
悬赏园豆:10 [待解决问题]

 public static Connection getMyConnection()//获取连接
    {
         
        try{
           Class.forName("com.mysql.jdbc.Driver").newInstance();//加载
          conn=DriverManager.getConnection(url, "root","123456");
          System.out.print("连接成功");
        }
        catch(Exception e)
        { e.printStackTrace();}
        return conn;
    }

当我调用getMyConnection(),调试到 Class.forName("com.mysql.jdbc.Driver").newInstance();就产生异常了,我不知道为什么??

木槿花的主页 木槿花 | 初学一级 | 园豆:109
提问于:2012-03-27 14:39
< >
分享
所有回答(4)
0

mysql的jar包放到正确的位置估计就行,如 classpath指定或者tomcat等的lib目录

2012 | 园豆:21230 (高人七级) | 2012-03-27 19:22
0

Class.forName("com.mysql.jdbc.Driver") 就直接加载驱动了吧,没必要在后面加上newInstance()。还有就是把catch里面改成System.out.println(e.getMessage());把详细异常打印出来。 不过JDBC找不到类这个异常,基本都是jar包问题。 

憤怒的小鳥 | 园豆:206 (菜鸟二级) | 2012-03-31 11:23
0
 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 
 5 public class ConnectionUtil {
 6     private static Connection conn = null;
 7     public static Connection getConnection(){
 8         Connection conn = null;
 9         try {
10             //通过java中Class类的forName将数据库的驱动程序加载
11             Class.forName("com.mysql.jdbc.Driver");
12             //通过DriverManager的getConnection返回一个Connection,将此连接赋值给conn对象;
13             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
14             System.out.println("数据库连接成功!");
15             return conn;
16         } catch (ClassNotFoundException e) {
17             e.printStackTrace();
18             System.out.println("数据库连接失败!");
19             return null;
20         } catch (SQLException e){
21             e.printStackTrace();
22             System.out.println("数据库连接失败!");
23             return null;
24         }
25     }
26 }
编程小神仙 | 园豆:233 (菜鸟二级) | 2012-04-28 10:16
0

jar包放错位置了  应该放在项目WEB-INFO 下lib里面

×jokey | 园豆:206 (菜鸟二级) | 2012-07-16 13:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册