2019년 겨울 특강 과제 1차 제출(2020-01-06)
- 내용 : 도서관리 완성
- 도서 추가
- 도서 목록
- 도서에 필요한 필드를 추가하여 완성 : 전주대학교 도서관 도서 목록 참조
- 필드 예 : 도서코드(text), 도서명(text), 저자(text), 가격(int), 출판사(text), ISBN(text), …
- Swing : JLabel, JTextField 외 JRadioButton, JCheckBox, JTextArea, JComboBox, JPanel, JMenu 등을 사용해보길..
- 제출일 : 2020-01-06일 24시까지
- 제출 방법 : 현재 홈페이지에 글쓰기
글쓰기 샘플
- 제목 : 홍길동 도서관리
- 사용 테이블 : tbook
- 공통 코드
- BookDTO.java
123456789101112131415package kr.co.moak.summer2019;public class BookDTO {String title;int pric;String publ;public BookDTO() {}public BookDTO(String title, int pric, String publ) {this.title = title;this.pric = pric;this.publ = publ;}}
- BookDAO.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283package kr.co.moak.summer2019;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;public class BookDAO {String tableName = "tbook";public Connection getConn() {Connection conn = null;try {Class.forName("org.sqlite.JDBC");conn = DriverManager.getConnection("jdbc:sqlite:" + MyConst.DB);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public void insert(BookDTO dto) {Connection conn = null;PreparedStatement ps = null;String sql = "INSERT INTO " + tableName + "(titl, pric, publ) VALUES (?, ?, ?);";conn = getConn();try {ps = conn.prepareStatement(sql);ps.setString(1, dto.title);ps.setString(2, dto.publ);ps.setInt(3, dto.pric);ps.executeUpdate();conn.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** 테이블의 캡션 출력 */public Vector<Object> getTableHeader() {Vector<Object> col = new Vector<Object>();col.add("_id");col.add("제목");col.add("가격");col.add("출판사");return col;}/** 도서목록 출력 */public Vector<Object> getListVector() {Vector<Object> data = new Vector<Object>();Connection con = null; // 연결PreparedStatement ps = null; // 명령ResultSet rs = null; // 결과try {con = getConn();String sql = "select * from" + tableName + "order by title asc;";ps = con.prepareStatement(sql);rs = ps.executeQuery();while (rs.next()) {Vector<String> row = new Vector<String>();row.add(rs.getInt("_id")); //이곳에서 오류발생 : 해결방법은?, 왜 생기는지?row.add(rs.getString("title"));row.add(rs.getString("pric"));row.add(rs.getString("publ"));data.add(row);} // while} catch (Exception e) {e.printStackTrace();}return data;}// getListVector()}
- BookDTO.java
- 도서 추가
- 실행화면
- InsertForm.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980package kr.co.moak.summer2019;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class InsertForm extends JFrame implements ActionListener{JTextField jfTitle;JTextField jfPric;JTextField jfPubl;public InsertForm() {this.setTitle("도서관리");this.setSize(800, 600);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLayout(null);//도서명JLabel lblNo = new JLabel("도서명 : ");lblNo.setBounds(10, 10, 100, 25);this.add(lblNo);jfTitle = new JTextField();jfTitle.setBounds(120, 10, 100, 25);this.add(jfTitle);//가격JLabel lblName = new JLabel("가격 : ");lblName.setBounds(10, 50, 100, 25);this.add(lblName);jfPric = new JTextField();jfPric.setBounds(120, 50, 100, 25);this.add(jfPric);// 출판사JLabel lblDept = new JLabel("출판사 : ");lblDept.setBounds(10, 90, 100, 25);this.add(lblDept);jfPubl = new JTextField();jfPubl.setBounds(120, 90, 100, 25);this.add(jfPubl);JButton btnSave = new JButton("저장");btnSave.setBounds(10, 500, 100, 50);this.add(btnSave);this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew InsertForm();}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubBookDAO dao;switch(e.getActionCommand()) {case "저장":String name = jfTitle.getText();int pric = Integer.parseInt(jfPric.getText());String dept = jfPubl.getText();BookDTO dto = new BookDTO(name, pric, dept);dao = new BookDAO();dao.insert(dto);break;}}}
- 실행화면
- 실행화면(도서 목록)
- 실행화면
- ListForm.java
1234567891011121314151617181920212223242526272829303132333435363738package kr.co.moak.summer2019;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class ListForm extends JFrame{DefaultTableModel model;JTable table;public ListForm() {this.setTitle("도서목록");this.setLayout(null);this.setResizable(false);this.setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(800, 600);BookDAO dao = new BookDAO();Vector<Object> vecs = dao.getListVector();Vector<Object> cols = dao.getTableHeader();model = new DefaultTableModel(vecs, cols); // model.setDataVector(vecs, cols);table = new JTable(model);JScrollPane scroll = new JScrollPane(table);scroll.setBounds(10, 10, 770, 500);this.add(scroll);this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew ListForm();}}
- 실행화면