[Spring] com.mysql.cj.jdbc.Driver 에러 해결

이진이·2023년 8월 10일
5
post-thumbnail

스프링 mysql 연동 에러

java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver 해결


진짜 5시간 걸려서 해결했다...😭

5시간 어쩌면 짧을수도 있지만, 그중 4시간은 찾는 내내 똑같은 답변들만 나와서 미치는줄..



스프링 김영한님의 강의를 다 보고 혼자 실습을 해보는데, 데이터 베이스 h2를 깔고싶지 않아서 이미 있던 mysql로 쓰려고 했다. 다른 블로그들을 참고하며 잘 따라 갔지만 에러.

  • jar파일이 잘 있느냐,
  • 8.0 이상이고 com.mysql.cj.jdbc.Driver 중간에 cj잘 들어 갔느냐,
  • 환경변수 설정이 잘 되었느냐,
  • 뒤에 띄어쓰기 없느냐

진짜 별의 별거 다해봤다. 심지어 mysql도 버전이 두개 깔려 있어서 다 지우고 8.0.32로 다시 깔았다.
근데도 안돼서 계속 삽질하던 중 한줄기 빛 등장
https://skyriv312079.tistory.com/35



이전 오류는 모르겠으나 내가 새로 받은 mysql 8.0.32 때문인지, 순전히 스프링의 버전 때문인지 어쨋든 mysql connection문이 바뀌었다.

implementation 'org.springframework.boot:spring-boot-starter'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

build.gradle > dependencies 부분에 해당 코드를 넣었더니 됐다.


전체 모습은
build.gradle

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'

   implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
   implementation 'org.springframework.boot:spring-boot-starter-jdbc'
   implementation 'com.h2database:h2'
   implementation 'org.springframework.boot:spring-boot-starter'
   runtimeOnly 'com.mysql:mysql-connector-j'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springtest?
serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=____




... 배고프다

profile
프론트엔드 공부합니다. 블로그 이전: https://jinijana.tistory.com

2개의 댓글

comment-user-thumbnail
2024년 4월 17일

헐...ㅠㅠ 저도 됐어요 감사합니다

답글 달기
comment-user-thumbnail
2024년 4월 24일

오 ㅠㅠ 저도 됐습니다 좋은 글 올려 주셔서 감사합니다 ㅠㅠ!!

답글 달기