MySQL JDBC driver 다운로드 & 설정

자주 잊어버리니 기록!

  1. MySQL Connector 8 로 MySQL 5.6, 5.7 에 접속 가능하므로 특별한 이유가 없다면 8 을 사용하는 것이 좋습니다.
  2. timezone 에러가 날 경우 MySQL JDBC 에러 해결 "The server time zone value 'KST' is unrecognized or represents more than one time zone. " 를 참고하세요.


Download

8.x

$ wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.22.zip

5.1.x

$ wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.49.zip

설정

maven

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>


gradle

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'


JDBC Driver class

com.mysql.cj.jdbc.Driver


MySQL 5.1 이하

 Click here to expand...
com.mysql.jdbc.Driver


JDBC URL format

jdbc:mysql://[host][,failoverhost...][:port]/[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...


property file

jdbc:mysql://localhost:3306/lesstif?useUnicode=true&characterEncoding=utf8


XML

& 가 특수 문자이므로 &amp; 로 변경

jdbc:mysql://localhost:3306/lesstif?useUnicode=true&amp;characterEncoding=utf8
  • useUnicode 의 기본 설정은 true 이므로 빼도 된다.
  • characterEncoding 의 기본 설정은 'autodetect' 다.

Spring DataSource 용

<bean id="myDS"
		class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/lesstif?useUnicode=true&amp;characterEncoding=utf8" />
		<property name="username" value="userid" />
		<property name="password" value="useriduserPwd" />
	</bean> 


Ref