java我正试图通过MacOSX上的JDBC连接mysql
我的IDE是Eclipse Indigo。当我尝试连接时,我发现:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
这是我的密码
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");
}
}
}
我认为JDBC不是进口的。我试着用计算机导入它
preference -> java -> build path -> user library -> add jars
但我还是有例外
# 1 楼答案
这不是在Eclipse中向类路径添加JAR的方式
你必须右键点击你的项目,选择
Java Build Path > Libraries
并添加一个JAR文件。对于MySQL,您需要MySQL Connector J