java连接数据库失败

运行下面代码段,一直提示驱动加载失败!
imp ort java.sql.*;
imp ort java.io.*;
public class Login {

  
    public static void main(String[] args)throws Exception,SQLException {        
    String url="jdbc:mysql://localhost:3306/phpwind_87_gbk";
    String username="root";
    String password="";
    Connection con=null;
    String  driver="com.mysql.jdbc.Driver";
    try{
          Class.forName(driver) ; 
          System.out.println("数据库加载成功!");
    }catch(ClassNotFoundException e1){
        e1.printStackTrace();
        System.out.println("驱动加载失败"+driver);
    }
    try{
        con=DriverManager.getConnection(url,username,password);
    }catch(SQLException e){
        e.printStackTrace();
        System.out.println("数据库连接失败!");
    }
    }

}


原因是:这个jar包没有加载,mysql-connector-java-5.1.7-bin.jar  
(1)首先下载mysql-connector-java-5.1.7-bin.jar  将其放在:C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext 这个路径下
(2)进入Eclipse的该工程目录下,点击Properties后弹出如下界面--再点击右侧的“Add External JARs” 将C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext 下的mysql-connector-java-5.1.7-bin.jar  引入进来,再次运行就OK了

猜你喜欢

转载自blog.csdn.net/qq_37537011/article/details/80381360