[Java] ํšŒ์› ๊ด€๋ฆฌ Program ๐Ÿง™โ€โ™€๏ธ

soyeonยท2022๋…„ 7์›” 22์ผ
0
post-thumbnail

ํšŒ์› ๊ด€๋ฆฌ Program

: ์ƒˆ๋กœ์šด ํšŒ์›์„ ๋“ฑ๋ก, ๊ธฐ์กด ํšŒ์›์„ ๊ฒ€์ƒ‰

์‚ฌ์šฉํ•  table ์ƒ์„ฑ

CREATE TABLE users (
	id		 VARCHAR(10) NOT NULL PRIMARY KEY,
    name 	 VARCHAR(20) NOT NULL,
    password VARCHAR(20) NOT NULL
);

Layered Architecture

  1. Main.java -> main method
  2. UserDAO.java -> DAO
  3. User.java -> DTO, VO, Entity, Bean

์ฝ”๋“œ

package lecture0722.step1;

import java.sql.SQLException;

public class Main {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		// Service ์—†์ด main์—์„œ ์ง์ ‘ ๋กœ์ง์ฒ˜๋ฆฌ๋ฅผ ํ•ด ๋ณด์•„์š”.
		UserDAO dao = new UserDAO();
		
		User user = new User();
		user.setId("1");
		user.setName("ํ™๊ธธ๋™");
		user.setPassword("test1234");
		
		dao.insert(user);
		
		System.out.println("์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๋“ฑ๋ก");
		
		User user2 = dao.select("1");
		System.out.println(user2.getName() + ", " + user2.getPassword());
	}
}

package lecture0722.step1;

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

public class UserDAO {

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		// ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ Exception์„ ๋˜์ง„๋‹ค.
		// try - catch๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {
		
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
	
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

package lecture0722.step1;

public class User {

	private String id;
	private String name;
	private String password;
	
	public User() {
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

class๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•˜๋Š” ๊ฒƒ

: ์œ ์ง€ ๋ณด์ˆ˜์„ฑ (์–ด๋–ค ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ ์ฝ”๋“œ๋ฅผ ๋น ๋ฅด๊ณ , ์‰ฝ๊ณ , ์•ˆ์ •์ ์œผ๋กœ ๋ณ€ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.)
-> ์ฝ”๋“œ์˜ ์ˆ˜์ •์ด ๋‹ค๋ฅธ ๋ถ€๋ถ„์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์„ ์ตœ์†Œํ™”
SoC(Seperation of Concern)
Refactoring -> method extraction ๊ธฐ๋ฒ•์„ ์ด์šฉํ•ด์„œ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์žฌ๊ตฌ์„ฑ ํ•ด๋ณด์ž

์ฝ”๋“œ

package lecture0722.step2;

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

public class UserDAO {
	
	private Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = getConnection();	
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = getConnection();	
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

class๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•˜๋Š” ๊ฒƒ

์žฌ์‚ฌ์šฉ์„ฑ์ด ์žˆ๋Š” class์ธ๊ฐ€์š”?
: ์œ„์—์„œ ๋งŒ๋“  DAO class๋ฅผ ํŒ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
ํ•˜์ง€๋งŒ getConnection()์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ๊ณ ์ •์‹œ์ผœ ๋†“์€ ์ƒํƒœ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ์‚ฐ ๊ณณ์—์„œ๋Š” ์ด๋ฏธ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์จ์•ผ ํ•œ๋‹ค.
=> ์ถ”์ƒ method
์ƒ์†์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

์ฝ”๋“œ

package lecture0722.step2;

import java.sql.SQLException;

public class Main {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		// Service ์—†์ด main์—์„œ ์ง์ ‘ ๋กœ์ง์ฒ˜๋ฆฌ๋ฅผ ํ•ด ๋ณด์•„์š”.
		UserDAO dao = new NUserDAO();
		
		User user = new User();
		user.setId("1");
		user.setName("ํ™๊ธธ๋™");
		user.setPassword("test1234");
		
		dao.insert(user);
		
		System.out.println("์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๋“ฑ๋ก");
		
		User user2 = dao.select("1");
		System.out.println(user2.getName() + ", " + user2.getPassword());
	}
}

package lecture0722.step2;

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

public abstract class UserDAO {
	
	protected abstract Connection getConnection() throws ClassNotFoundException, SQLException;

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = getConnection();
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = getConnection();	
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

package lecture0722.step2;

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

// naver ์ชฝ ๊ฐœ๋ฐœ์ž๊ฐ€ UserDAO๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด
// subclass๋ฅผ ์ž‘์„ฑ
public class NUserDAO extends UserDAO {

	@Override
	protected Connection getConnection() throws ClassNotFoundException, SQLException {
		// naver ๋‚˜๋ฆ„๋Œ€๋กœ์˜ connection ์—ฐ๊ฒฐ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ตฌํ˜„์ด ๋‚˜์˜ค๋ฉด ๋œ๋‹ค.
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}
}

์ƒ์œ„ class(UserDAO)์— ๊ธฐ๋ณธ์ ์ธ logic ํ๋ฆ„์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค. ์ด ์•ˆ์— protected method, abstract method๊ฐ€ ์žˆ๋‹ค. ํ•˜์œ„ class(NUserDAO)์—์„œ ์ถ”์ƒ method๋ฅผ overridingํ•ด์„œ ํ•˜์œ„ class์— ๋งž๋„๋ก ๊ธฐ๋Šฅ์„ ์™„์„ฑํ•œ๋‹ค.
=> Design pattern์—์„œ Template method pattern (Framework์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.)

-> class๋ฅผ ์ƒ์†ํ•ด์„œ ํ™•์žฅํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ์‹์ด๋‹ค.
-> ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ(logic)์€ ์ƒ์œ„ class์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค. ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์€ ํ•˜์œ„ class์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

์กฐ๊ธˆ ๋‹ค๋ฅธ ๊ด€์ ์—์„œ ์‚ดํŽด๋ณด๋ฉด

๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ด€์ ์—์„œ ๋‹ค์‹œ ์‚ดํŽด๋ณธ๋‹ค.
UserDAO์—๋Š” getConnection์ด๋ผ๋Š” ์ถ”์ƒ method๊ฐ€ ์กด์žฌํ•œ๋‹ค. ํ•˜์œ„ class์ธ NUserDAO์—์„œ ๊ตฌ์ฒด์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๊ณ  ์žˆ๋‹ค.
=> ํ•˜์œ„ class์—์„œ ๊ฐ์ฒด์˜ ๊ตฌ์ฒด์ ์ธ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” Design pattern์„ Factory method pattern์ด๋ผ๊ณ  ํ•œ๋‹ค.

์ƒ์†์€ ๋‚˜๋ฆ„๋Œ€๋กœ์˜ ์ œ์•ฝ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์ž.
class๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ๋งŒ๋“ ๋‹ค.

์ฝ”๋“œ

package lecture0722.step3;

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

public class UserDAO {
	
	private SimpleConnectionMaker simpleConnectionMaker;
	
	public UserDAO() {
		simpleConnectionMaker = new SimpleConnectionMaker();
	}

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = simpleConnectionMaker.makeNewConnection();
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = simpleConnectionMaker.makeNewConnection();
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

package lecture0722.step3;

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

public class SimpleConnectionMaker {

	public Connection makeNewConnection() throws ClassNotFoundException, SQLException{	
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}
}

-> ์ˆ˜ํ–‰์€ ์ž˜ ๋˜์ง€๋งŒ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•˜์ง€ ์•Š๋Š” ์ด์ƒ ์žฌ์‚ฌ์šฉ์ด ํž˜๋“ค๋‹ค.
UserDAO๋Š” SimpleConnectionMaker class์— ์ข…์†๋œ๋‹ค. ๋‘ class๊ฐ€ ์•„์ฃผ ๊ฐ•ํ•˜๊ฒŒ ๋ฌถ์ธ๋‹ค.(tigtly coupled ํ˜„์ƒ ๋ฐœ์ƒ) ์˜คํžˆ๋ ค ์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ๋ณด๋‹ค ์•ˆ ์ข‹์•„์กŒ๋‹ค.

=> ํ•œ class ์•ˆ์— ๋‹ค๋ฅธ class ์ด๋ฆ„์ด ๋ฐ•ํ˜€์„œ๋Š” ์•ˆ๋œ๋‹ค.

์–ด๋–ป๊ฒŒ ์œ„์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ• ๊นŒ?

: Interface

์ฝ”๋“œ

package lecture0722.step4;

import java.sql.Connection;
import java.sql.SQLException;

public interface ConnectionMaker {

	public Connection makeConnection() throws ClassNotFoundException, SQLException;
}

package lecture0722.step4;

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

public class NUserDAO implements ConnectionMaker {

	@Override
	public Connection makeConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}
}

package lecture0722.step4;

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

public class UserDAO {
	
	private ConnectionMaker connectionMaker;
	
	public UserDAO() {
		connectionMaker = new NUserDAO();
	}

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = connectionMaker.makeConnection();
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = connectionMaker.makeConnection();
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

-> UserDAO class์—์„œ NUserDAO class ์ด๋ฆ„์ด ๋‹ค์‹œ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋‹ค.
์•„์ง ๋งŒ๋“ค์ง€๋„ ์•Š์€ class๊ฐ€ ์ฝ”๋“œ์—์„œ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

์–ด๋–ป๊ฒŒ ์œ„์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ• ๊นŒ?

: DI(Dependency Injection)

์ฝ”๋“œ

package lecture0722.step5;

import java.sql.Connection;
import java.sql.SQLException;

public interface ConnectionMaker {

	public Connection makeConnection() throws ClassNotFoundException, SQLException;
}

package lecture0722.step5;

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

public class NUserDAO implements ConnectionMaker {

	@Override
	public Connection makeConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}
}

package lecture0722.step5;

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

public class UserDAO {
	
	private ConnectionMaker connectionMaker;
	
	// ๊ฐ์ฒด๋ฅผ ์ฃผ์ž… ๋ฐ›๋Š”๋‹ค.
	public UserDAO(ConnectionMaker connectionMaker) {
		this.connectionMaker = connectionMaker;
	}

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = connectionMaker.makeConnection();
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = connectionMaker.makeConnection();
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

package lecture0722.step5;

import java.sql.SQLException;

public class Main {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		// Service ์—†์ด main์—์„œ ์ง์ ‘ ๋กœ์ง์ฒ˜๋ฆฌ๋ฅผ ํ•ด ๋ณด์•„์š”.
		ConnectionMaker connectionMaker = new NUserDAO();
		
		UserDAO dao = new UserDAO(connectionMaker);
		
		User user = new User();
		user.setId("1");
		user.setName("ํ™๊ธธ๋™");
		user.setPassword("test1234");
		
		dao.insert(user);
		
		System.out.println("์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๋“ฑ๋ก");
		
		User user2 = dao.select("1");
		System.out.println(user2.getName() + ", " + user2.getPassword());
	}
}

-> UserDAO class๊ฐ€ ๋‹ค๋ฅธ class์— ์ข…์†๋˜์ง€ ์•Š๋Š”๋‹ค.

Java Program์€ ์ƒ์† ๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๋ผ Interface ๊ธฐ๋ฐ˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

Layered Arcitecture + interface + javaFx

package lecture0722.user;

public class User {

	private String id;
	private String name;
	private String password;
	
	public User() {
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

package lecture0722.user;

import java.sql.Connection;
import java.sql.SQLException;

public interface ConnectionMaker {

	public Connection makeConnection() throws ClassNotFoundException, SQLException;
}

package lecture0722.user;

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

public class NUserDAO implements ConnectionMaker {

	@Override
	public Connection makeConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String jdbc_URL = "jdbc:mysql://localhost:3306/sqldb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
		Connection con = DriverManager.getConnection(jdbc_URL, "root", "password");
		return con;
	}
}

package lecture0722.user;

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

public class UserDAO {
	
	private ConnectionMaker connectionMaker;
	
	// ๊ฐ์ฒด๋ฅผ ์ฃผ์ž… ๋ฐ›๋Š”๋‹ค.
	public UserDAO(ConnectionMaker connectionMaker) {
		this.connectionMaker = connectionMaker;
	}

	public void insert(User user) throws ClassNotFoundException, SQLException { 
		Connection con = connectionMaker.makeConnection();
		
		String sql = "INSERT INTO users VALUES(?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getId());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getPassword());
		
		pstmt.executeUpdate();
		
		pstmt.close();
		con.close();
	}
	
	public User select(String id) throws ClassNotFoundException, SQLException {		
		Connection con = connectionMaker.makeConnection();
		
		String sql = "SELECT * FROM users WHERE id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		pstmt.close();
		con.close();
		
		return user;
	}
}

package lecture0722.user;

import java.sql.SQLException;

public class UserService {
	
	private ConnectionMaker connectionMaker;
	private UserDAO dao;
	
	public UserService() {
		connectionMaker = new NUserDAO();
		dao = new UserDAO(connectionMaker);
	}
	
	public void pushUser(User user) throws ClassNotFoundException, SQLException {
		dao.insert(user);
	}
	
	public User findUser(String id) throws ClassNotFoundException, SQLException {
		return dao.select(id);
	}
}

package lecture0722.user;

import java.sql.SQLException;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {
	TextArea textarea;
	Button insertBtn, selectBtn;
	TextField idField, nameField, passwordField;
		
	@Override
	public void start(Stage primaryStage) throws Exception {
				
		UserService userService = new UserService();
		
		// ํ™”๋ฉด ๊ตฌ์„ฑ
		BorderPane root = new BorderPane();
		root.setPrefSize(700, 500);  // window ํฌ๊ธฐ
			
		textarea = new TextArea();
		root.setCenter(textarea);  // ํ™”๋ฉด center์— textarea๋ฅผ ๋ถ™์ธ๋‹ค.
		
		insertBtn = new Button("User ์‚ฝ์ž…");
		insertBtn.setPrefSize(150, 40);
		insertBtn.setOnAction(e -> {
			textarea.clear();
			
			User user = new User();
			user.setId(idField.getText());
			user.setName(nameField.getText());
			user.setPassword(passwordField.getText());
			
			try {
				userService.pushUser(user);
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

			textarea.appendText("์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๋“ฑ๋ก");
			idField.clear();
			nameField.clear();
			passwordField.clear();
		});
		
		idField = new TextField();
		idField.setPrefSize(50, 40);
		nameField = new TextField();
		nameField.setPrefSize(140, 40);
		passwordField = new TextField();
		passwordField.setPrefSize(140, 40);
		
		selectBtn = new Button("User ๊ฒ€์ƒ‰");
		selectBtn.setPrefSize(150, 40);
		selectBtn.setOnAction(e -> {	
			textarea.clear();
					
			User user = null;
			try {
				user = userService.findUser(idField.getText());
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			textarea.appendText(user.getName() + ", " + user.getPassword());
			idField.clear();
		});
	
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10,10,10,10));  // ์—ฌ๋ฐฑ์„ ์ค€๋‹ค.
		flowPane.setPrefSize(700, 40);
		flowPane.setHgap(10);
		flowPane.getChildren().add(insertBtn);  // ๋ฒ„ํŠผ ๋ถ€์ฐฉ
		flowPane.getChildren().add(idField);
		flowPane.getChildren().add(nameField);
		flowPane.getChildren().add(passwordField);
		flowPane.getChildren().add(selectBtn);
			
		root.setBottom(flowPane);
		
		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		launch();
	}
}

์‹คํ–‰ํ™”๋ฉด

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