java.lang.ClassNotFoundException:com.mysql.jdbc.Driver错误在执行以下代码时发生

时间:2019-02-08 06:29:18

标签: java mysql jdbc

package jdbc3;

import java.sql.*;    
import java.sql.DriverManager;

public class InsertPrepared {

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/people?autoReconnect=true&useSSL=false","root","manish13595");
            PreparedStatement stmt=con.prepareStatement("insert into epmloyes values(?,?)");
            stmt.setInt(1, 101);
            stmt.setString(2, "Ratan");
            int i=stmt.executeUpdate();
        //  ResultSet rs=stmt.executeQuery();
        //  System.out.println(rs.getString(1)+ " " +rs.getString(2));
            System.out.println(i+"records insert");
            PreparedStatement stmt1=con.prepareStatement("select * from emp");  
            ResultSet rs=stmt1.executeQuery();  
            while(rs.next()){  
            System.out.println(rs.getInt(1)+" "+rs.getString(2));  
            }  
            con.close();
        }catch(Exception e) {
            System.out.println(e);
        }    
    }    
}

2 个答案:

答案 0 :(得分:0)

请在您的类路径中添加mysql-connector.jar。

有关更多信息:

http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html#ixzz2Ply4zLFF

答案 1 :(得分:0)

如果您使用maven进行依赖项管理,只需在pom.xml中将依赖项添加到mysql驱动程序中即可:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>