이번 포스팅에서는 DBCP를 알아보고, 스프링부트에서 MySQL을 연동하는 과정을 담았습니다.
src
main
java
com.corini
MySQLRunner.java
SpringbootApplication.java
resources
static
templates
application.properies
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
spring.datasource.hikari.maximum-pool-size=4
spring.datasource.url=디비주소
spring.datasource.username=디비접속아이디
spring.datasource.password=디비비밀번호
디비 주소 입력할 때, jdbc:mysql://
스킴을 앞에 붙이는 것을 잊지마세요 ㅎㅎ;
예를 들면 jdbc:mysql://localhost:3306
이런 식으로요!
package org.lym.springbootmysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.Connection;
@Component
public class MySQLRunner implements ApplicationRunner {
@Autowired
DataSource dataSource;
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(ApplicationArguments args) throws Exception {
try(Connection connection = dataSource.getConnection()) {
System.out.println(connection.getClass());
System.out.println(connection.getMetaData().getURL());
System.out.println(connection.getMetaData().getUserName());
}
}
}