티스토리 뷰
728x90
반응형
개발 환경
java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)
Tomcatv 10.1
EclipseVersion: 2024-06 (4.32.0)Build id: 20240606-1231
JDBC란
JDBC(Java Database Connectivity)는 자바 애플리케이션에서 다양한 종류의 데이터베이스에 접속하고,
SQL 쿼리를 실행하여 데이터를 주고받을 수 있도록 해주는 표준 API입니다.
web.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
id="WebApp_ID" version="5.0">
<display-name>com.mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<!-- 스프링의 ContextLoaderListener 등록 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 스프링 설정 파일 (web-applicationContext.xml)의 위치 설정 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-applicationContext.xml</param-value>
</context-param>
</web-app>
web-applicationContext.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- dataSource 설정 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" />
<property name="username" value="system" />
<property name="password" value="1234" />
</bean>
<!-- JdbcTemplate 설정 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
</beans>
Controller 설정
package com.mvc.controller;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.mvc.member.MemberVO;
@Controller
public class DataAccessController {
@Autowired
@Qualifier("jdbcTemplate")
private final JdbcTemplate jdbcTemplate;
@RequestMapping(value="/springJdbc", method=RequestMethod.GET)
public ModelAndView springJdbc() {
ModelAndView modelAndView = new ModelAndView("dataAccess");
modelAndView.addObject("message", "this is a spring JDBC.");
String query = "select * from t_member";
List<MemberVO> memberList = new ArrayList<MemberVO>();
memberList = this.jdbcTemplate.query(query, new RowMapper<MemberVO>() {
public MemberVO mapRow(ResultSet resultSet, int rowNum) throws SQLException{
MemberVO memberVO = new MemberVO();
memberVO.setId(resultSet.getString("id"));
memberVO.setPwd(resultSet.getString("pwd"));
return memberVO;
}
});
for(MemberVO item: memberList) {
System.out.println(item.getId());
}
return modelAndView;
}
}
추가 정보
스프링 프레임워크(Spring FrameWordk)-Spring Web MVC
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- nl2br
- .submit()
- In App Purchase
- java-개발 환경 설정하기
- 표현 언어(expression language)
- jstl(java standard tag library)-core
- jsp 오픈 소스
- java.sql
- MainActor
- 진수 변환
- 특정 문자를 기준으로 자르기
- 스프링 시큐리티(spring security)
- java 키워드 정리
- await
- system.io
- java web-mvc
- 문자 자르기
- 스프링 프레임워크(spring framework)
- System.Diagnostics
- jstl(java standard tag library)
- 상품 등록
- error-java
- 람다식(lambda expression)
- 스프링 시큐리티(spring security)-http basic 인증
- 스프링 프레임워크(spring framewordk)
- async
- 메이븐(maven)
- 인텔리제이(intellij)
- REST API
- 제품 등록
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함