JAVA DB SQLITE UPDATE

Posted by PeEn
2019. 7. 30. 16:19 Programing/Java

메인 코드

메인 메서드에서 값을 DTO클레스에 있는 생성자로 보내준 후 LibrarySystem_DAO클레스에서 DTO에 임시 저장한 값들을 update매서드로 가져와 sql 구문을 이용해 해당 _id값을 찾아 Update하게 된다

	case "수정":
			_id = tfID.getText();
			num = tfNum.getText();
			name = tfName.getText();
			phone = tfPhone.getText();
			temp = tfTemp.getText();
			identity = tfIdentity.getText();

			
			LibrarySystem_DTO dtoUp = new LibrarySystem_DTO(_id,num,name,phone,temp,identity);
			
			dao = new LibrarySystem_DAO();
			dao.SELECT_ID(dtoUp);
			dao.UPDATE(dtoUp);
		

			tfID.setText("");
			tfNum.setText("");
			tfName.setText("");
			tfPhone.setText("");
			tfTemp.setText("");
			tfIdentity.setText("");

			model.setDataVector(dao.getListVector(), dao.getTableHeader());
		
		}
	}

Update 매서드

	//__________UPDATE__________//
				public void UPDATE(LibrarySystem_DTO dto) {
					Connection conn = null;
					PreparedStatement ps = null;
					
					String sql = "UPDATE "+MemberstableName + " SET  (num, name, phone, temp, identity) = (?, ?, ?, ?, ?) WHERE _id = ?;";
					conn = getConn();
					try {
						ps = conn.prepareStatement(sql);
						ps.setString(1, dto.num);
						ps.setString(2, dto.name);
						ps.setString(3, dto.phone);
						ps.setString(4, dto.temp);
						ps.setString(5, dto.identity);
						ps.setString(6, dto._id);
						
						ps.executeUpdate();
						conn.close();
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
    }

 

LibrarySystem_DTO 생성자

public class LibrarySystem_DTO {
	String _id;
	String num;
	String name;
	String phone;
	String temp;
	String identity;
	
	public LibrarySystem_DTO() {
		// TODO Auto-generated constructor stub
	}
	
	public LibrarySystem_DTO(String _id) {
		this._id = _id;
	}
	
	public LibrarySystem_DTO(String num, String name, String phone, String temp, String identity ) {
		this.num = num;
		this.name = name;
		this.phone = phone;
		this.temp = temp;
		this.identity = identity;
	}
	
	public LibrarySystem_DTO(String _id , String num, String name, String phone, String temp, String identity ) {
		this._id = _id;
		this.num = num;
		this.name = name;
		this.phone = phone;
		this.temp = temp;
		this.identity = identity;
	}
 }

'Programing > Java' 카테고리의 다른 글

JAVA DB SQLITE DELETE  (0) 2019.07.30
JAVA DB SQLITE INSERT  (0) 2019.07.30
ReportH201562054 전기요금 계산 프로그램 연습  (0) 2019.06.11
ReportF201562054 도서 저장 프로그램 연습  (0) 2019.06.11
ReportF, ReportH 파일  (0) 2019.06.08