: ์๋ก์ด ํ์์ ๋ฑ๋ก, ๊ธฐ์กด ํ์์ ๊ฒ์
CREATE TABLE users (
id VARCHAR(10) NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL
);
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();
}
}