l ๐Ÿค– ์ฝ”๋“œ l

๊ธฐ์ƒ์ฒญ ๋‚ ์”จ api ๋ฐ›์•„์™€์„œ db์— ์ €์žฅํ•˜๊ธฐ (DAO)2 (1์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์ €์žฅํ•˜๊ธฐ)

์ฝ”๋“œ๊ทธ๋ฆผ 2020. 5. 21.
๋ฐ˜์‘ํ˜•

๊ธฐ์ƒ์ฒญ api๋Š” 3์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ๋‚ ์”จ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์–ด์š”.

๊ทธ๊ฑฐ๋ฅผ 1์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์งœ๋ณธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

 

//ํ”„๋กœ์ ํŠธ ํ†ต์œผ๋กœ ์˜ฌ๋ ค๋‘” ๊นƒํ—ˆ๋ธŒ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ํ™•์ธ ํ•ด ์ฃผ์„ธ์š”.

github.com/Joowon0220/weather.git

package com.weather03.mvc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.weather03.db.JDBCTemplate.*;

public class VillageWeatherDAO {

	public int insertVillageWeather(List<VillageWeather> vw) throws ClassNotFoundException {

		
		Connection con = null;
		
		ResultSet rs = null;
		
		PreparedStatement insert_pstm = null;
		PreparedStatement insert_pstm2 = null;
		PreparedStatement insert_pstm3 = null;
		int res = 0;

		con = getConnection();

		

		String query = "INSERT INTO WEATHER03" + " VALUES(getdate(),?,?,?,?)";
		String query2 = "INSERT INTO WEATHER03" + " VALUES(getdate(),?,?,?,?)";
		String query3 = "INSERT INTO WEATHER03" + " VALUES(getdate(),?,?,?,?)";

		try {
			for (int i = 0; i < vw.size(); i++) {
				VillageWeather str = vw.get(i);

				insert_pstm = con.prepareStatement(query);
				insert_pstm.setString(1, str.getBaseDate());
				insert_pstm.setString(2, str.getBaseTime());
				insert_pstm.setString(3, str.getT3h());
				insert_pstm.setString(4, str.getReh());

				System.out.println("3.query ready");
				res = insert_pstm.executeUpdate();

				if (res > 0) {
					commit(con);
					System.out.println("์ธ์„œํŠธ ์„ฑ๊ณต");
				}

				
				
				System.out.println("4.return");
			}
			for (int i = 0; i < vw.size(); i++) {
				VillageWeather str = vw.get(i);
				int baseTime2 = Integer.parseInt(str.getBaseTime())+100;
				String new_baseTime2 = String.format("%04d", baseTime2);
				insert_pstm2 = con.prepareStatement(query2);
				insert_pstm2.setString(1, str.getBaseDate());
				insert_pstm2.setString(2, new_baseTime2);
				insert_pstm2.setString(3, str.getT3h());
				insert_pstm2.setString(4, str.getReh());

				System.out.println("3.query ready");
				res = insert_pstm2.executeUpdate();

				if (res > 0) {
					commit(con);
					System.out.println("์ธ์„œํŠธ ์„ฑ๊ณต");
				}

				
				
				System.out.println("4.return");
			}
			for (int i = 0; i < vw.size(); i++) {
				VillageWeather str = vw.get(i);
				int baseTime3 = Integer.parseInt(str.getBaseTime())+200;
				String new_baseTime3 = String.format("%04d", baseTime3);
				insert_pstm3 = con.prepareStatement(query3);
				insert_pstm3.setString(1, str.getBaseDate());
				insert_pstm3.setString(2, new_baseTime3);
				insert_pstm3.setString(3, str.getT3h());
				insert_pstm3.setString(4, str.getReh());

				System.out.println("3.query ready");
				res = insert_pstm3.executeUpdate();

				if (res > 0) {
					commit(con);
					System.out.println("์ธ์„œํŠธ ์„ฑ๊ณต");
				}

				
				
				System.out.println("4.return");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("3 or 4 error");
			e.printStackTrace();
		} finally {
			close(insert_pstm, con);
		}
		return res;
		
		

		// else ๋

	}
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

๐Ÿ’ฒ ์ถ”์ฒœ ๊ธ€