JDBC, MySQL ์—ฐ๋™ 3 - INSERT

JOYยท2021๋…„ 12์›” 13์ผ
0

๏ปฟSQL & JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
10/12
post-thumbnail
post-custom-banner

๐Ÿ“Œ JDBC, MySQL ์—ฐ๋™ 3 - INSERT


1. MySQL ์—ฐ๋™

RoleDao.java - addRole()

๐Ÿ“ RoleDao.java๋Š” MySQL๊ณผ Role.java ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค ์ด๋‹ค.

1. JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ๋ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ

  • JDBC API ์‚ฌ์šฉ์„ ์œ„ํ•ด Class.forName ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ

  • DB ๋งˆ๋‹ค ํด๋ž˜์Šค ์ด๋ฆ„์ด ๋‹ค๋ฅด๋ฏ€๋กœ ํ•ด๋‹น DB ์ด๋ฆ„์„ ์ •ํ™•ํ•˜๊ฒŒ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •

//๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ
Class.forName("com.mysql.jdbc.Driver");
//์ปค๋„ฅ์…˜ ๊ฐ์ฒด
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
//insert ๋œ ๊ฒฐ๊ณผ ๊ฑด์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ผ int ํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
int insertCount = 0;
		
Connection conn = null;
PreparedStatement ps = null;
  • insert๋ฌธ์€ ๊ฒฐ๊ณผ๊ฐ’์„ ResultSet์œผ๋กœ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ X

2. SQL๋ฌธ ์ž‘์„ฑ

String sql = "insert into role(role_id, description) values (?, ?)";
			
ps = conn.prepareStatement(sql);
			
ps.setInt(1, role.getRoleId());
ps.setString(2, role.getDescription())

insertCount = ps.executeUpdate(); //๋ช…๋ ์–ด ์‹คํ–‰
  • executeUpdate() : ์ฒ˜๋ฆฌํ•œ ๋กœ์šฐ์˜ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜

JDBCexam2.java

2. JDBC - INSERT

  • ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์ „ role ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ

๐Ÿ™ role_id ์€ 500, description ์€ "CTO" ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•ด๋ณด์ž.

package kr.or.connect.jdbcexam;

import kr.or.connect.jdbcexam.dao.RoleDao;
import kr.or.connect.jdbcexam.dto.Role;

public class JDBCexam2 {

	//Insert
	public static void main(String[] args) {
		int roleId = 500;
		String description = "CTO";
		
		Role role = new Role(roleId, description);
		
		RoleDao dao = new RoleDao();
		int insertCount = dao.addRole(role);
		
		System.out.println(insertCount);
	}

}
  • ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ํ›„ role ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ

๋งˆ์ง€๋ง‰ ํ–‰์— ์ •์ƒ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž… ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.



๐Ÿ“• ์ „์ฒด ์ฝ”๋“œ

RoleDao.java - addRole()

package kr.or.connect.jdbcexam.dao;

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

import kr.or.connect.jdbcexam.dto.Role;

public class RoleDao {
	
	private static String dburl = "jdbc:mysql://localhost:3306/connectdb1?serverTimezone=Asia/Seoul&useSSL=false";
	private static String dbUser = "connectuser";
	private static String dbpasswd = "";
	

	
	//Insert
	public int addRole(Role role) {
		int insertCount = 0;
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
			
			String sql = "insert into role(role_id, description) values (?, ?)";
			
			ps = conn.prepareStatement(sql);
			
			ps.setInt(1, role.getRoleId());
			ps.setString(2, role.getDescription());
			
			insertCount = ps.executeUpdate();
			
		} catch (Exception e) {			
			e.printStackTrace();
		}finally {
			if(ps!=null) {
				try {
					ps.close();
				} catch (SQLException e) {					
				}
				
			if(conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
			}
		}
		
		
		
		return insertCount;
	}

JDBCexam2.java

package kr.or.connect.jdbcexam;

import kr.or.connect.jdbcexam.dao.RoleDao;
import kr.or.connect.jdbcexam.dto.Role;

public class JDBCexam1 {

	public static void main(String[] args) {
		RoleDao dao = new RoleDao();		
		Role role = dao.getRole(100);

		System.out.println(role);
	}
}

profile
Just Do IT ------- ๐Ÿƒโ€โ™€๏ธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€