Part2. 도서관리에서는 도서관리 프로그램 관련해 설명하겠습니다.


Book DTO

package BookData_p;

 
public class BookData{ // name 책이름, author 저자, num 도서번호, publish1 출판, publish2 출간, Borrowing 대여확인
	private String name,author, publish1, publish2;
	private int num;
	private String Borrowing;
	public BookData() {}
	
	public BookData(String name, String Borrowing){
		this.name = name;
		this.Borrowing = Borrowing;
	}
	
	public BookData(int num, String Borrowing){
		this.num = num;
		this.Borrowing = Borrowing;
	}
	public BookData(int num, String name, String author, String publish1, String publish2, String Borrowing) 
	{
		this.num = num;
		this.name = name;
		this.author = author;
		this.publish1 = publish1;
		this.publish2 = publish2;
		this.Borrowing = Borrowing;
	}
	public int GetNum() 
	{
		return num;
	}
	public void SetNum(int num)
	{
		this.num = num;
	}
	public String Getname()
	{
		return name;
	}
	public void Setname(String name)
	{
		this.name = name;
	}
	public String GetAuthor() 
	{
		return author;
	}
	public void Setauthor(String author)
	{
		this.author = author;
	}
	public String Getpublish1() 
	{
		return publish1;
	}
	public void Setpublish1(String publish1) 
	{
		this.publish1 = publish1;
	}
	public String Getpublish2() 
	{
		return publish2;
	}
	public void Setpublish2(String publish2) 
	{
		this.publish2 = publish2;
	}
	public String GetBorrowing() 
	{
		return Borrowing;
	}
	public void SetBorrowing(String publish2) 
	{
		this.Borrowing = publish2;
	}
}

 


Book DAO

package BookDataDB_p;

import java.util.*;
import java.sql.*;

import BookData_p.BookData;

public class BookDataDB{
	private Connection conn = null;
	private ResultSet rs = null;
	private Statement st = null;
	private PreparedStatement ps = null;
	
	public BookDataDB() { // 생성자로 데이터베이스 연결
		try {
			final String url = "jdbc:mariadb://localhost:3306/bookdb";
			final String id = "root";
			final String pw = "1234";
			
			Class.forName("org.mariadb.jdbc.Driver");
			conn = DriverManager.getConnection(url, id, pw);
			
		}catch(ClassNotFoundException cnfe) {
			System.out.println("DB 드라이버 로딩 실패:"+ cnfe.toString());
		}catch(SQLException sqle){
			System.out.println("DB 접속실패"+ sqle.toString());
		}catch(Exception e){
			System.out.println("Unkown error");
			e.printStackTrace();
		}
	}
	
	public void DBClose() { // 커넥션 연결 종료
		try {
			if(rs != null) rs.close();
			if(st != null) st.close();
			if(ps != null) ps.close();
		} catch (Exception e) {
			System.out.println(e + " => DBClose fail");
		}
	}

 

// 도서 정보 저장	
	public void InsertBook(BookData bookdata) { // book_management table -> book data insert
		try {
			String sql = "insert into book_management values(?, ?, ?, ?, ?,?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, bookdata.GetNum());
			ps.setString(2, bookdata.Getname());
			ps.setString(3, bookdata.GetAuthor());
			ps.setString(4, bookdata.Getpublish1());
			ps.setString(5, bookdata.Getpublish2());
			ps.setString(6, bookdata.GetBorrowing());
			ps.executeUpdate();
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			DBClose();
		}
	}

 

// 도서정보 목록
	public Vector<BookData> Booklist()  // book_management table -> book data list
	{
		Vector<BookData> Ar = new Vector<BookData>();
		 
		 try{
			 st = conn.createStatement();
			 
			 String sql = "Select * From book_management order by Number*1";
			 rs = st.executeQuery(sql);
			 
			 while (rs.next()) {
				 Ar.add(new BookData(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));
			 }
		 }catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBClose();
			}
		 return Ar;
		 
	}

Vector 사용하여 데이터형을 BookData(DTO)으로 정하였고, DB에 있는 도서 정보를 불러와 저장하는 메소드입니다.

 

// 도서정보 업데이트
	public void UpdateBook(BookData bookdata)  // book_management table -> book data update
	{
		try {
			String Updata = "update book_management set Book=? , author =?, publish1=?,publish2=?, B =? where Number = ?;";
			ps = conn.prepareStatement(Updata);
			ps.setString(1, bookdata.Getname());
			ps.setString(2, bookdata.GetAuthor());
			ps.setString(3, bookdata.Getpublish1());
			ps.setString(4, bookdata.Getpublish2());
			ps.setString(5, bookdata.GetBorrowing());
			ps.setInt(6, bookdata.GetNum());
			ps.executeUpdate();
			}catch(SQLException e){	
				e.printStackTrace();
			} finally {
				DBClose();
			}
	}

도서정보를 수정하는 메소드입니다.

 

//도서 정보 삭제
	public void Delete(int Num)  // book_management table -> book data delete
	{
		String Delete = "delete from book_management where Number = ?;";
		try {
			ps = conn.prepareStatement(Delete);
			ps.setInt(1, Num);
			ps.executeUpdate();
		}catch (SQLException e) {
			e.printStackTrace();
		} finally {
				DBClose();
		}	
	}

도서 정보를 삭제하는 메소드이다.

 

// 도서 대여 정보 저장
	public void BorrowingInsert(BookData bookdata)  // book_management table -> borrowing book data insert
	{
		try {
			String Updata = "update book_management set B =? where Number=?;";
			ps = conn.prepareStatement(Updata);
			ps.setString(1, bookdata.GetBorrowing());
			ps.setInt(2, bookdata.GetNum());
			ps.executeUpdate();
			}catch(SQLException e){	
				e.printStackTrace();
			} finally {
				DBClose();
			}
	}

도서대여 확인 하는 변수인 B(Borrowing)를 업데이트 하는 메소드입니다.

 

// 도서 대여 확인
	public int ConfirmBorrowing(BookData bookdata){  // book_management table -> borrowing book data confirm
		String sql = "select * from book_management where Number = ? and B = ?";
		try{
			
		ps = conn.prepareStatement(sql);
		ps.setInt(1, bookdata.GetNum());
		ps.setString(2, bookdata.GetBorrowing());
		rs = ps.executeQuery();
		
		if(rs.next()) {
			
			return 1;		
		}else 
		{
			 
		}
		
		}catch(Exception e) { e.printStackTrace();}
		return -1;
	}

도서가 대여되었는지 확인하는 메소드입니다.

 

// 도서번호 확인	
	public int ConfirmBook(BookData bookdata){  // book_management table -> borrowing book data confirm
		String sql = "select * from book_management where Number = ?";
		try{
			
		ps = conn.prepareStatement(sql);
		ps.setInt(1, bookdata.GetNum());
		rs = ps.executeQuery();
		
		if(rs.next()) 
		{
			return 1;
			
		}
		
		}catch(Exception e) { e.printStackTrace();}
		return -1;
	}

도서번호가 중복되었는지 확인하는 메소드입니다.

 

// 도서명 확인	
	public String Bringbookname(BookData bookdata){ // book_management table -> borrowing bookname data Bring 
		String sql = "select * from book_management where Number = ?";
		try{
			
		ps = conn.prepareStatement(sql);
		ps.setInt(1, bookdata.GetNum());
		rs = ps.executeQuery();
		
		if(rs.next()) 
		{
			
			
			System.out.println(rs.getString(2));
			return rs.getString(2);
		}
		
		
		}catch(Exception e) { e.printStackTrace();}
		return "";
	}
}

 

도서명을 가져오기 위한 메소드입니다.


Book WindowBuilder

package WindowBuilder_p;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import BookData_p.BookData;
import BookDataDB_p.BookDataDB;
import borrowing_management.Borrowing_;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class WindowBuilder {
	
	
	
	public JFrame frame;
	private JTable table;
	private JTextField Numtext;
	private JTextField Nametext;
	private JTextField Authortext;
	private JTextField publish1text;
	private JTextField publish2text;
	
	String colNames[] = {"도서 번호","도서명","저자","출판","출간","대여 현황"};
	private DefaultTableModel model = new DefaultTableModel(colNames, 0);

	public WindowBuilder() {
		initialize();
		select();
		KeyF5();
	}
	
	
	BookDataDB db = new BookDataDB();
	
	public void select() { // 도서 목록 출력
			Vector<BookData> Ar = new Vector<BookData>();
			Ar = db.Booklist();
		
			for(int i=0; i< Ar.size();i++)
			{
			model.addRow(new Object[]{Ar.get(i).GetNum(),Ar.get(i).Getname(),Ar.get(i).GetAuthor(),Ar.get(i).Getpublish1(),Ar.get(i).Getpublish2(),Ar.get(i).GetBorrowing()});
			}	
			
	}
	
	public void KeyF5(){ // F5 클릭 시 새로고침
		frame.getContentPane().addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				int keyCode = e.getKeyCode();
				System.out.println(keyCode);
				System.out.println(KeyEvent.VK_F5);
				if(keyCode==KeyEvent.VK_F5) 
				{
					model.setRowCount(0);
					select();
					
				}
			}
		});
		frame.getContentPane().setFocusable(true);
		frame.getContentPane().requestFocus();
	}
	
	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 1079, 645);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 0, 1063, 606);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setFont(new Font("함초롱바탕", Font.PLAIN, 18));
		scrollPane.setBounds(306, 106, 745, 466);
		panel.add(scrollPane);
		
		table = new JTable(model){
			public boolean isCellEditable(int row, int column) { // 클릭 비활성화
		        return false;
			}
		};
		table.addMouseListener(new MouseAdapter() { // 테이블 마우스 더블 클릭 시 도서정보 입력칸에 값이 나타남
			@Override
			public void mouseClicked(MouseEvent e) {
				DefaultTableModel model2 = (DefaultTableModel)table.getModel();
				int row = table.getSelectedRow();
				
					Numtext.setText(String.valueOf(model2.getValueAt(row, 0)));
					Nametext.setText(String.valueOf(model2.getValueAt(row, 1))); 
					Authortext.setText(String.valueOf(model2.getValueAt(row, 2))); 
					publish1text.setText(String.valueOf(model2.getValueAt(row, 3))); 
					publish2text.setText(String.valueOf(model2.getValueAt(row, 4)));
			}
		});
		table.setBackground(Color.WHITE);
		table.setFont(new Font("함초롱바탕", Font.PLAIN, 16));
		
		//테이블 가운데 정렬
		DefaultTableCellRenderer cell = new DefaultTableCellRenderer();
		cell.setHorizontalAlignment(SwingConstants.CENTER);
		TableColumnModel centerModel = table.getColumnModel();
		for(int i=0;i < centerModel.getColumnCount(); i++) centerModel.getColumn(i).setCellRenderer(cell);
		
		//테이블 컬럼의 이동을 방지
				table.getTableHeader().setReorderingAllowed(false);      
				table.getColumnModel().getColumn(0).setPreferredWidth(20);
				table.getColumnModel().getColumn(0).setResizable(false);
				table.getColumnModel().getColumn(1).setPreferredWidth(162);
				table.getColumnModel().getColumn(3).setPreferredWidth(40);
				
		scrollPane.setViewportView(table);
		
		JButton InputGetbookButton = new JButton("도서 등록");
		InputGetbookButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		InputGetbookButton.addActionListener(new ActionListener() { // 도서 등록 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent arg0) {
				
				int confirmbook = db.ConfirmBook(new BookData(Integer.parseInt(Numtext.getText()),""));
				if(confirmbook == 1) {
					JOptionPane.showMessageDialog(null, "도서 번호가 중복 되었습니다.");
					return;
				}
				
				if(Numtext.getText().length() == 0 || Nametext.getText().length() == 0 || 
						Authortext.getText().length() == 0 || publish1text.getText().length() == 0 || publish2text.getText().length() == 0) 
				{
					JOptionPane.showMessageDialog(null, "입력이 제대로 되어 있지 않습니다.");
					return;
				}
				
				
				db.InsertBook(new BookData(Integer.parseInt(Numtext.getText()), Nametext.getText(), Authortext.getText(),
						publish1text.getText(), publish2text.getText(), "대여 가능"));
	
				model.setRowCount(0);
				JOptionPane.showMessageDialog(null, "등록 완료");
				Numtext.setText(""); Nametext.setText(""); Authortext.setText(""); publish1text.setText(""); publish2text.setText("");
				select();
			}
		});
		InputGetbookButton.setBounds(377, 11, 107, 33);
		panel.add(InputGetbookButton);
		
		JButton BooklistButton = new JButton("새로고침"); 
		BooklistButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		BooklistButton.addActionListener(new ActionListener() { // 새로고침 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent arg0) {
				model.setRowCount(0);
				select();
			}
		});
		BooklistButton.setBounds(136, 10, 107, 35);
		panel.add(BooklistButton);
		
		JButton InputUpdatebutton = new JButton("도서 수정");
		InputUpdatebutton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		InputUpdatebutton.addActionListener(new ActionListener() { // 도서 수정 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent e) {
				int confirmborrowing = db.ConfirmBorrowing((new BookData(Integer.parseInt(Numtext.getText()),"대여 불가"))); //대여 가능 도서 확인
				
				if(Numtext.getText().length() ==  0|| Nametext.getText().length() ==  0 || 
						Authortext.getText().length() == 0 || publish1text.getText().length() == 0 || publish2text.getText().length() == 0) // 도서정보 미 입력 확인
				{
					JOptionPane.showMessageDialog(null, "입력이 제대로 되어 있지 않습니다.");
					return;
					
				}
				
				System.out.println("nUM: "+Numtext.getText().length());
				
				if(confirmborrowing == 1) // ConfirmBorrowing 메소드 반환 값이 1이면 즉, 대여된 도서이면 대여 불가로 도서 정보 업데이트
				{	
					
					
					
					db.UpdateBook(new BookData(Integer.parseInt(Numtext.getText()), Nametext.getText(), Authortext.getText(), 
							publish1text.getText(), publish2text.getText(), "대여 불가"));
					
					Numtext.setText(""); Nametext.setText(""); Authortext.setText(""); publish1text.setText(""); publish2text.setText("");
					model.setRowCount(0);
					JOptionPane.showMessageDialog(null, "수정 완료");
					select();
					return;
				} else { db.UpdateBook(new BookData(Integer.parseInt(Numtext.getText()), Nametext.getText(), Authortext.getText(), 
						publish1text.getText(), publish2text.getText(), "대여 가능"));
				
				Numtext.setText(""); Nametext.setText(""); Authortext.setText(""); publish1text.setText(""); publish2text.setText("");
				model.setRowCount(0);
				JOptionPane.showMessageDialog(null, "수정 완료");
				select(); }
				
				
				
			}
		});
		InputUpdatebutton.setBounds(496, 10, 107, 34);
		panel.add(InputUpdatebutton);
		
		JButton TableDeleteButton = new JButton("도서 삭제");
		TableDeleteButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		TableDeleteButton.addActionListener(new ActionListener() { // 도서 삭제 버튼 클릭 시
			public void actionPerformed(ActionEvent e) {
				
				int result = JOptionPane.showConfirmDialog(null, "장말 삭제 하시겠습니까?", "확인", JOptionPane.YES_NO_OPTION);
				int confirmborrowing = db.ConfirmBorrowing(new BookData(Integer.parseInt(Numtext.getText()),"대여 불가"));
				
				
				if(confirmborrowing == 1) // 대여 중인 도서 이면
				{
					JOptionPane.showMessageDialog(null, "대여 중이므로 삭제 불가능 합니다.");
					return;
				}else
				
				if(result == JOptionPane.CANCEL_OPTION) { // 팝업 취소하면
					return;
				}
				else if(result == JOptionPane.YES_NO_OPTION){
					
					System.out.println(e.getActionCommand());
					DefaultTableModel model2 = (DefaultTableModel)table.getModel();
					
					db.Delete((int)model2.getValueAt(table.getSelectedRow(), 0));
					model2.removeRow(table.getSelectedRow());
					model.setRowCount(0);
					Numtext.setText(""); Nametext.setText(""); Authortext.setText(""); publish1text.setText(""); publish2text.setText("");
					JOptionPane.showMessageDialog(null, "삭제 완료");
					select();
				}
				else {
					JOptionPane.showMessageDialog(null, "삭제 취소");
				}
				
				
			}
		});
		
		TableDeleteButton.setBounds(615, 10, 107, 35);
		panel.add(TableDeleteButton);
		
		JButton exitButton = new JButton("종료");
		exitButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		exitButton.addActionListener(new ActionListener() { // 종료 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent e) {
				int result = JOptionPane.showConfirmDialog(null, "정말 종료 하시겠습니까?", "확인", JOptionPane.YES_NO_OPTION);
				if(result == JOptionPane.CANCEL_OPTION) {
					return;
				}
				else if(result == JOptionPane.YES_NO_OPTION){
					System.exit(0);
				}
				else {
					return;
				}

			}
		});
		exitButton.setBounds(954, 10, 97, 35);
		panel.add(exitButton);
		
		JButton textFieldResetButton = new JButton("입력 초기화");
		textFieldResetButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		textFieldResetButton.addActionListener(new ActionListener() { // 입력 초기화 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent e) {
				Numtext.setText(""); Nametext.setText(""); Authortext.setText(""); publish1text.setText(""); publish2text.setText("");
			}
		});
		textFieldResetButton.setBounds(17, 10, 107, 35);
		panel.add(textFieldResetButton);
		
		JLabel Numlabel = new JLabel("도서 번호");
		Numlabel.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		Numlabel.setBounds(12, 160, 93, 23);
		panel.add(Numlabel);
		
		JLabel Namelabel = new JLabel("도서명");
		Namelabel.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		Namelabel.setBounds(33, 209, 60, 23);
		panel.add(Namelabel);
		
		Numtext = new JTextField();
		
		Numtext.setToolTipText("도서 번호 입력");
		Numtext.setFont(new Font("맑은 고딕", Font.PLAIN, 17));
		Numtext.setColumns(10);
		Numtext.setBounds(105, 157, 186, 30);
		panel.add(Numtext);
		
		Nametext = new JTextField();
		Nametext.setToolTipText("도서명 입력");
		Nametext.setFont(new Font("맑은 고딕", Font.PLAIN, 17));
		Nametext.setColumns(10);
		Nametext.setBounds(105, 210, 186, 30);
		panel.add(Nametext);
		
		Authortext = new JTextField();
		Authortext.setFont(new Font("맑은 고딕", Font.PLAIN, 17));
		Authortext.setToolTipText("저자 입력");
		Authortext.setColumns(10);
		Authortext.setBounds(105, 260, 186, 30);
		panel.add(Authortext);
		
		JLabel Authorlabel = new JLabel("저자");
		Authorlabel.setHorizontalAlignment(SwingConstants.CENTER);
		Authorlabel.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		Authorlabel.setBounds(33, 259, 60, 23);
		panel.add(Authorlabel);
		
		JLabel Publish1label = new JLabel("출판");
		Publish1label.setHorizontalAlignment(SwingConstants.CENTER);
		Publish1label.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		Publish1label.setBounds(33, 308, 60, 23);
		panel.add(Publish1label);
		
		publish1text = new JTextField();
		publish1text.setFont(new Font("맑은 고딕", Font.PLAIN, 17));
		publish1text.setToolTipText("출판 입력");
		publish1text.setColumns(10);
		publish1text.setBounds(105, 309, 186, 30);
		panel.add(publish1text);
		
		publish2text = new JTextField();
		publish2text.setFont(new Font("맑은 고딕", Font.PLAIN, 17));
		publish2text.setToolTipText("출간 입력");
		publish2text.setColumns(10);
		publish2text.setBounds(105, 360, 186, 30);
		panel.add(publish2text);
		
		JLabel Publish2label = new JLabel("출간");
		Publish2label.setHorizontalAlignment(SwingConstants.CENTER);
		Publish2label.setFont(new Font("맑은 고딕", Font.BOLD, 20));
		Publish2label.setBounds(33, 359, 60, 23);
		panel.add(Publish2label);
		
		JButton button = new JButton("대여프로그램");
		button.addActionListener(new ActionListener() { // 대여프로그램 버튼 클릭 시 동작
			public void actionPerformed(ActionEvent e) {
				
				Borrowing_ window = new Borrowing_();  // 대여프로그램 GUI 실행
				window.frame.setVisible(true); // 대여프로그램 GUI 나타남
				window.frame.setResizable(false); // GUI 창 크기 조절 불가능
				
			}
		});
		button.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		button.setBounds(377, 54, 107, 33);
		panel.add(button);

	}
}

실행화면

도서관리 프로그램 GUI

 

도서 수정 버튼 클릭
도서 수정 후

 

Part3. 대여프로그램에서는 대여프로그램 버튼 클릭시 나타나는 GUI 에 관련하여 설명하겠습니다.


Part1. 로그인 바로가기

Part3. 대여프로그램 바로가기

 

전체 소스코드 바로가기

+ Recent posts