我正在尝试通过mac osx上的JDBC连接mysql

yoy*_*sir 1 java mysql eclipse macos jdbc

我的IDE是Eclipse Indigo.当我尝试连接时,我得到了这个:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

这是我的代码.

public class TPCH
{
    public static void main(String[] args)
    {
        String userName = "tpch";
        String password = "tpch";
        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", userName);
        connectionProps.put("password", password);

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(
                           "jdbc:mysql://localhost:3306/",
                           connectionProps);
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            System.out.println("Error connecting to db");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为JDBC不是导入的.我尝试导入它

preference -> java -> build path -> user library -> add jars
Run Code Online (Sandbox Code Playgroud)

但我仍然有例外.

ada*_*shr 5

这不是你如何在Eclipse中的类路径中添加JAR.

您必须右键单击您的项目,选择Java Build Path > Libraries并添加JAR文件.对于MySQL,你需要的MySQL的连接口J.

在此输入图像描述