336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tansanc.tistory; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; class Info { public String title; public String publisher; public String year; public int price; public long contentLength = 0L; public Info(String title, String publisher, String year, int price) { this.title = title; this.publisher = publisher; this.year = year; this.price = price; } } class InfoTableModel extends AbstractTableModel { private static final long serialVersionUID = 7932826462497464190L; public ArrayListpages; public InfoTableModel() { pages = new ArrayList (); } public int getColumnCount() { return 4; } public int getRowCount() { return pages.size(); } public void addInfo(Info page) { int idx = pages.size(); pages.add(page); fireTableRowsInserted(idx, idx); // 반드시 호출해야한다. } public Object getValueAt(int rowIndex, int columnIndex) { Info info = pages.get(rowIndex); switch (columnIndex) { case 0: return info.title; case 1: return info.publisher; case 2: return info.year; case 3: return info.price; case 4: return info.contentLength; default: return "invalid"; } } } class myFrame extends JFrame { JTable table; public myFrame() { // TODO Auto-generated constructor stub setSize(600, 400); setVisible(true); InfoTableModel model = new InfoTableModel(); TableColumnModel columnModel = new DefaultTableColumnModel(); TableCellRenderer renderer = new DefaultTableCellRenderer(); // 기본구현 model.addInfo(new Info("1", "1", "1", 50000)); try { Connection con = ConnectDatabase.makeConnection(); Statement stmt; stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM books"); while (rs.next()) { int book_id = rs.getInt("book_id"); String title = rs.getString("title"); String publisher = rs.getString("publisher"); String year = rs.getString("year"); int price = rs.getInt("price"); System.out.println(title + publisher + year + price); model.addInfo(new Info(title, publisher, year, price)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done"); TableColumn column = new TableColumn(0); column.setCellRenderer(renderer); // 이렇게 하지 않아도 알아서 제공된다. column.setHeaderValue("title"); columnModel.addColumn(column); column = new TableColumn(1); column.setHeaderValue("publisher"); columnModel.addColumn(column); column = new TableColumn(2); column.setHeaderValue("year"); columnModel.addColumn(column); column = new TableColumn(3); column.setHeaderValue("price"); columnModel.addColumn(column); table = new JTable(model, columnModel); add(table); } } public class ConnectDatabase { public static Connection makeConnection() { String url = "jdbc:mysql://localhost/book_db"; String id = "root"; String password = "root"; Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("드라이버 적재 성공"); con = DriverManager.getConnection(url, id, password); System.out.println("데이터베이스 연결 성공"); } catch (ClassNotFoundException e) { System.out.println("드라이버를 찾을 수 없습니다."); } catch (SQLException e) { System.out.println("연결에 실패하였습니다."); } return con; } public static void main(String[] args) { makeConnection(); new myFrame(); } }
'Programming > JAVA,JSP' 카테고리의 다른 글
JAVA 소수를 판단하는 GUI 프로그램 (0) | 2013.12.21 |
---|---|
JAVA 버블 정렬 (0) | 2013.11.30 |
JAVA 채팅 프로그램 소스 (0) | 2013.08.23 |
JAVA 채팅 클라이언트 (0) | 2013.08.23 |
JAVA 계산기 프로그램 부분완성 (0) | 2013.08.16 |