티스토리 뷰

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

 

스프링 프레임워크(Spring FrameWordk)-Spring Web MVC

사전 정보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) Tomcat v10.1Eclipse Version: 2024-06 (4.32.0) Dynamic Web Pr

jangjeonghun.tistory.com

 

728x90
반응형