반응형
pom.xml 입력 후 Project 우클릭 - Maven - Reimport 클릭(일정 시간 소요) => dependencies 빨간줄 사라짐.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kr.or.connect</groupId>
<artifactId>daoexam01</artifactId>
<version>1.0-SNAPSHOT</version>
<name>daoexam</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- basic data source -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
main 데이터베이스 접속방법.
package kr.or.connect.daoexam01.main;
import kr.or.connect.daoexam01.config.ApplicationConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
public class DataSourceTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
DataSource ds = ac.getBean(DataSource.class);
Connection conn = null;
try {
conn = ds.getConnection();
if (conn != null)
System.out.println("접속 성공^^");
} catch (Exception e) {
System.out.println("실패");
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
DBconfig.class
package kr.or.connect.daoexam01.config;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
public class DBconfig {
private String driverClassName = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:포트번호/데이터베이스이름?useUnicode=true&characterEncoding=utf8";
private String username = "유저이름";
private String password = "비밀번호!";
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
ApplicationConfig.class
package kr.or.connect.daoexam01.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(DBconfig.class)
public class ApplicationConfig {
}
위와 같이 입력 성공시, 아래와 같은 화면이 출력된다.
반응형
'웹(Web) > 백엔드(Backend)' 카테고리의 다른 글
백엔드 로드맵 (0) | 2021.01.04 |
---|---|
파이썬 구글 이미지 크롤링(uh oh! keywords is a required argument, unfortunately all 20 could not be downloaded because some images were not downloadable. 0 is all we got for this search filter!) (3) | 2020.12.16 |
Maven이란? CoC이란? (0) | 2020.05.31 |
(Servlet/JSP) redirect와 forward (0) | 2020.05.27 |
(Servlet/JSP) setCharacterEncoding, setContentType에서 한글깨짐 문제 해결(UTF-8) (0) | 2020.05.22 |