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

๊ธฐ์ƒ์ฒญ ๋‚ ์”จ api ๋ฐ›์•„์™€์„œ db์— ์ €์žฅํ•˜๊ธฐ (JDBC Template)

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

db์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„!!

 

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

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

github.com/Joowon0220/weather.git

package com.weather03.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTemplate {

	public static Connection getConnection() {
		Connection con = null;
		String url = "jdbc:sqlserver://localhost:1433;" + "databaseName=weather;";
		String user = "sa"; // DB ID
		String password = "0000"; // DB Password

		try { // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("1. driver connect");
		} catch (ClassNotFoundException e) {
			System.out.println("1. driver error");
			e.printStackTrace();
		}

		try {
			con = DriverManager.getConnection(url, user, password);
			con.setAutoCommit(false);
			System.out.println("2. user connect");
		} catch (SQLException e) {
			System.out.println("2. user error");
			e.printStackTrace();
		}

		return con;
	}

	public static void commit(Connection con) {
		try {
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void rollback(Connection con) {
		try {
			con.rollback();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

	public static void close(ResultSet rs, PreparedStatement pstm, Connection con) {
		try {
			rs.close();
			pstm.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(PreparedStatement insert_pstm, Connection con) {
		try {
			insert_pstm.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

 

 

 

 

 

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์ด๋‚˜ ์ถ”๊ฐ€ ์ฝ”๋“œ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์ œ ๊นƒํ—ˆ๋ธŒ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”๐Ÿ‘ฉ๐Ÿ’ซ๐Ÿงก 

 

https://github.com/Joowon0220

 

Joowon0220 - Overview

๐™„ ๐™๐™ค๐™ฅ๐™š ๐™ข๐™ฎ ๐™˜๐™ค๐™™๐™š'๐™จ ๐™ฌ๐™–๐™ง๐™ข๐™ฉ๐™ ๐™ฉ๐™ค๐™ช๐™˜๐™๐™š๐™จ ๐™ฎ๐™ค๐™ช :) . Joowon0220 has 14 repositories available. Follow their code on GitHub.

github.com

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

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