java.lang.ClassNotFoundException : Eclipse의 com.mysql.jdbc.Driver
코드의 문제점은 디버깅 중에 많은 오류가 있습니다. mysql 데이터베이스에 연결할 싱글 톤 클래스에 대한 코드를 작성 중입니다.
내 코드는 다음과 같습니다.
package com.glomindz.mercuri.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySingleTon {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
private static MySingleTon myObj;
private Connection Con ;
private MySingleTon() {
System.out.println("Hello");
Con= createConnection();
}
@SuppressWarnings("rawtypes")
public Connection createConnection() {
Connection connection = null;
try {
// Load the JDBC driver
Class driver_class = Class.forName(driver);
Driver driver = (Driver) driver_class.newInstance();
DriverManager.registerDriver(driver);
connection = DriverManager.getConnection(url + dbName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return connection;
}
/**
* Create a static method to get instance.
*/
public static MySingleTon getInstance() {
if (myObj == null) {
myObj = new MySingleTon();
}
return myObj;
}
public static void main(String a[]) {
MySingleTon st = MySingleTon.getInstance();
}
}
나는 자바를 처음 사용합니다. 도와주세요.
mysql 연결 라이브러리가 프로젝트에 포함 되어 있지 않은 것 같습니다 . 제안 된 솔루션 중 하나에 따라 문제를 해결하십시오.
- 메이븐 프로젝트 솔루션
mysql-connector 종속성을 pom.xml 프로젝트 파일에 추가합니다.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
여기에 모든 버전이 있습니다 : https://mvnrepository.com/artifact/mysql/mysql-connector-java
- 모든 프로젝트 솔루션
프로젝트에 jar 라이브러리를 수동으로 추가하십시오.
프로젝트-> 빌드 경로-> 빌드 경로 구성을 마우스 오른쪽 단추로 클릭하십시오.
에서 Libraries Tab 키를 누릅니다 Add External Jar 과 Select당신의 항아리.
여기서 mysql-connector 용 zip을 찾을 수 있습니다.
- 설명:
프로젝트를 빌드 할 때 mysql 연결 라이브러리의 파일 (com.mysql.jdbc.Driver 클래스)을 찾을 수 없기 때문에 java에서 예외가 발생합니다. 솔루션은 라이브러리를 프로젝트에 추가하고 Java는 com.mysql.jdbc.Driver를 찾습니다.
IDE에 오류 (컴파일 시간 오류)가 발생하면 mysql-connector jar 파일을 libs에 추가하고이를 참조 된 프로젝트 라이브러리에도 추가해야합니다.
실행할 때이 오류가 발생하면 웹 서버의 lib 폴더에 mysql-connector JAR 파일을 포함하지 않았기 때문일 수 있습니다.
추가 mysql-connector-java-5.1.25-bin.jar웹 서버의 lib 디렉토리에 또한 클래스 패스합니다. Tomcat lib 경로가 예제로 제공됩니다.Tomcat 6.0\lib
들어 Gradle을이 프로젝트를 기반 당신에 대한 종속성이 필요 MySQL의 자바 커넥터 :
dependencies {
compile 'mysql:mysql-connector-java:6.0.+'
}
클래스 경로 에 MySQL MySQL Connector Jar 용 드라이버 jar를 포함해야합니다 .
Eclipse를 사용하는 경우 : Eclipse에서 종속 라이브러리를 추가하는 방법
명령 줄을 사용하는 경우 java의 -cp 매개 변수를 사용하여 드라이버 jar의 경로를 포함합니다.
java -cp C:\lib\* Main
여기에서(mysql-connector-java-bin) 클래스 경로 다운로드에서 jar 를 확인 하십시오.
모든 사람이 답을 썼지 만 실제로 가장 간단한 방법으로 답한 사람이 없다는 사실에 여전히 놀랍습니다.
사람들은 jar 파일을 포함하는 대답입니다. 그러나 오류는 계속 발생합니다.
그 이유는 프로젝트가 실행될 때 jar가 배포되지 않기 때문입니다. 따라서 우리가해야 할 일은 IDE에이 jar도 배포하도록 지시하는 것입니다.
여기 사람들은 WEB-INF의 lib 폴더에 jar 파일을 넣을 정도로 여러 번 대답했습니다. 괜찮아 보이지만 수동으로하는 이유는 무엇입니까? 간단한 방법이 있습니다. 아래 단계를 확인하십시오.
1 단계 : 프로젝트에서 jar 파일을 참조하지 않은 경우 다음과 같이 참조하십시오.
프로젝트를 마우스 오른쪽 버튼으로 클릭하고 프로젝트 속성으로 이동합니다. 그런 다음 Java 빌드 경로로 이동 한 다음이를 통해 외부 jar 파일을 추가하십시오.
그러나 빌드 경로를 통해 외부 jar를 추가하면 클래스 컴파일에만 도움이되며 프로젝트를 실행할 때 jar가 배포되지 않으므로 여전히 문제가 해결되지 않습니다. 이를 위해이 단계를 따르십시오.
프로젝트를 마우스 오른쪽 버튼으로 클릭하고 프로젝트 속성으로 이동합니다. 그런 다음 Deployment Assembly 로 이동 한 다음 Add 를 누른 다음 java 빌드 경로 항목으로 이동하여 jstl, mysql 또는 다른 jar 파일이든 라이브러리를 추가하십시오. 배포에 추가합니다. 아래는 그것을 표시하는 두 사진입니다.
JDBC API는 대부분 데이터베이스와 독립적으로 작동하는 인터페이스로 구성됩니다. JDBC API를 구현하는 각 데이터베이스에는 데이터베이스 특정 드라이버가 필요합니다.
먼저 www.mysql.com에서 MySQL 커넥터 jar를 다운로드 한 다음 :
Right Click the project -- > build path -- > configure build path
라이브러리 탭에서 누르고 Add External Jar항아리를 선택하십시오.
Maven 기반 프로젝트의 경우 종속성이 필요합니다.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
프로젝트에서 라이브러리 라이브러리-> 마우스 오른쪽 버튼 클릭-> 라이브러리 추가-> Mysqlconnector 5.1
The driver connector is not in your build path. Configure the build path and point it to the 'mysql-connector-java-5.1.25-bin.jar' (check the version which you are using). Alternatively you can use maven :D
Trivial as it may seem in my case netbeans version maven project 7.2.1 was different. There is a folder in the project called dependencies. Right click and then it brings up a popup window where you can search for packages. In the query area put
mysql-connector
It will bring up the matches (it seems it does this against some repository). Double click then install.
It is because the WEB-INF folder does not exist at the location in the sub directory in the error. You either compile the application to use the WEB-INF folder under public_html OR copy the WEB-INF folder in sub folder as in the error above.
For IntelliJ Idea, go to your project structure (File, Project Structure), and add the mysql connector .jar file to your global library. Once there, right click on it and chose 'Add to Modules'. Hit Apply / OK and you should be good to go.
The exception can also occur because of the class path not being defined.
After hours of research and literally going through hundreds of pages, the problem was that the class path of the library was not defined.
Set the class path as follows in your windows machine
set classpath=path\to\your\jdbc\jar\file;.
I Understood your problem add this dependency in your pom.xml your problem will be solved,
https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38
If you are using tomcat then along with project directory you should also copy the database connector jar file to tomcat/lib. this worked for me
'Program Club' 카테고리의 다른 글
| Dalvik 형식으로 변환 실패 : dex를 실행할 수 없음 : Java 힙 공간 (0) | 2020.10.08 |
|---|---|
| JavaScript를 통해 쿼리 문자열 값이 있는지 확인하는 방법은 무엇입니까? (0) | 2020.10.08 |
| .NET에서 생성 날짜별로 파일 가져 오기 (0) | 2020.10.08 |
| UITableViewController에서 UISearchDisplayController를 사용할 때 어설 션 실패 (0) | 2020.10.08 |
| NPM이 멈춰서 동일한 오류가 발생합니다. EISDIR : 디렉터리에 대한 잘못된 작업, 오류시 읽기 (기본) (0) | 2020.10.08 |

