[Giúp đỡ] Kết nối vào Mysql trong java

Nhoc con PTIT

Member
15/7/13
49
2
8
25
Em đang học phần kết nối mysql với java nhưng khi em chạy chương trình thì nó báo lỗi java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. Ai giúp em với. Dưới đây là code của em
Code:
package javaapplication5;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnectToDatabase {

    private Connection connect = null;
    private Statement statement = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    String url = "jdbc:mysql://localhost:3306/bookmanagement";
    String user = "root";
    String password = "admin";

    public void readDataBase() throws Exception {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection(url, user, password);
            statement = connect.createStatement();

            resultSet = statement.executeQuery("select * from employee");
            writeResultSet(resultSet);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    private void writeResultSet(ResultSet resultSet) throws SQLException {
        while (resultSet.next()) {
            int id = resultSet.getInt("emp_id");
            String name = resultSet.getString("emp_name");
            String position = resultSet.getString("emp_position");
            System.out.println("ID: " + id);
            System.out.println("Name: " + name);
            System.out.println("Position: " + position);
        }
    }

    public static void main(String[] args) {
        try {
            ConnectToDatabase connectDb = new ConnectToDatabase();
            connectDb.readDataBase();
        } catch (Exception ex) {
            Logger.getLogger(ConnectToDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}