ClassNotFoundException java
我对班级没有发现异常表示怀疑。我已经在我的文件中添加了所有的罐子,但我似乎无法使它工作。我已经在我的项目中添加了mysql-connector.jar。有人知道如何纠正吗ClassNotFoundException java,java,mysql,classnotfoundexception,Java,Mysql,Classnotfoundexception,我对班级没有发现异常表示怀疑。我已经在我的文件中添加了所有的罐子,但我似乎无法使它工作。我已经在我的项目中添加了mysql-connector.jar。有人知道如何纠正吗 try { String driverName = "com.mysql.jdbc.Driver"; Class.forName(driverName); String serverName = "localhost"; String mydatabase = "UFRJSocial";
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "localhost";
String mydatabase = "UFRJSocial";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root";
String password = "";
connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
status = ("STATUS--->Connected!");
} else {
status = ("STATUS--->The connection failed");
}
return connection;
} catch (ClassNotFoundException e) {
System.out.println("The specified driver was nor found");
//THIS IS THE LINE THAT MY CONSOLE IS SHOWING
return null;
} catch (SQLException e) {
System.out.println("it was not possible to connect to the database.");
return null;
}
请查看此代码:
enter code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import app.dao.connector.DatabaseManager;
public class MySQLDatabaseManager implements DatabaseManager{
@Override
public Connection getConnection() {
final String USER_NAME ="root";
final String PASSWORD = "1234";
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String JDBC_URL = "jdbc:mysql://localhost:3306/inventory system";
Connection conn = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(JDBC_URL, USER_NAME, PASSWORD);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
如您所见,我试图在try-and-catch块之外声明所有变量。在声明JDBC_URL时,请指定您的数据库。我已设法使其正常工作。Web INF文件夹中缺少mysql.jar文件 你是如何运行你的程序的?您的类路径设置为什么?您可能想检查一下如何使用Java连接到MySQL数据库。我正在Eclipse jee上用Tomcat7运行它。这段代码不是我的,所以我实际上还找不到我的类路径设置为什么。我将在这里搜索错误的类路径,这就是我所能说的,我如何才能使它正确?这样你就可以标记出答案,是吗?您可以自己添加一个答案并检查它。