解答例 - 実習課題2 - 1.JDBC API
(実習課題2)
以下のGUIアプリケーションを作成しなさい。
- ウィンドウに含まれるコンポーネントは、テーブルのみ。
- データベースに接続し、「product」テーブルのデータをテーブルに使用させること。
解答例
▼データベースアクセス用クラスのソース
package com.techscore.jdbc.chapter1.exercise2;
/**
* CoonectDataBase.java
* TECHSCORE JDBC1章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.List;
public class ProductDAO{
private Connection getConnection() throws SQLException,ClassNotFoundException{
Connection conn = null;
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://dbserver:5432/Training"
,"postgres" //ユーザ名
,""); //パスワード
return conn;
}
public List loadSummary() throws SQLException,ClassNotFoundException{
List list = new LinkedList();
Connection conn = null;
try{
conn = getConnection();
final String sql = "select p_num, p_name, type, price" +
" from product";
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
while (result.next()){
Product data = new Product(
result.getInt(1),
result.getString(2),
result.getString(3),
result.getInt(4));
list.add(data);
}
result.close();
statement.close();
}finally{
if (conn != null){
conn.close();
}
}
return list;
}
}
▼テーブル表示ウィンドウ作成クラスのソース
package com.techscore.jdbc.chapter1.exercise2;
/**
* DisplayProductTable.java
* TECHSCORE JDBC1章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.util.Iterator;
import java.util.List;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
public class DisplayProductTable extends JPanel {
public DisplayProductTable() {
super(new GridLayout(1,0));
//テーブルのタイトル
String[] columnNames = {"製品ナンバー",
"製品名",
"製品のタイプ",
"製品の価格"};
try{
ProductDAO productDAO = new ProductDAO();
List list = (List)productDAO.loadSummary();
//テーブルデータ内容
Object[][] data = new Object[list.size()][4];
Iterator ite=list.iterator();
for(int i=0; ite.hasNext(); i++){
Product product = (Product)ite.next();
data[i][0] = new Integer(product.getPNum()).toString();
data[i][1] = product.getPName();
data[i][2] = product.getType();
data[i][3] = new Integer(product.getPrice()).toString();
}
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(400, 70));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}catch(SQLException e){
e.printStackTrace();
System.exit(1);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.exit(1);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("製品情報テーブル表示");
DisplayProductTable newContentPane = new DisplayProductTable();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
▼Product(製品)情報管理クラスのソース
package com.techscore.jdbc.chapter1.exercise2;
/**
* Product.java
* TECHSCORE JDBC1章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
public class Product {
private String pName;
private int pNum;
private int price;
private String type;
public Product(){
}
public Product(int pNum,String pName,
String type,int price){
this.pName = pName;
this.pNum = pNum;
this.price = price;
this.type = type;
}
public String getPName() {
return pName;
}
public int getPrice() {
return price;
}
public String getType() {
return type;
}
public int getPNum() {
return pNum;
}
public void setPName(String string) {
this.pName = string;
}
public void setPrice(int i) {
this.price = i;
}
public void setType(String string) {
this.type = string;
}
public void setPNum(int pNum) {
this.pNum = pNum;
}
}
▼テスト用テーブル作成ファイル
/**
* TECHSCORE JDBC1章 実習課題2
* テスト用テーブル作成ファイル
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
* --テーブル構成--
* product: 製品管理表
* p_num 製品ナンバー
* p_name 製品名
* type 製品のタイプ
* price 製品の価格 (単位: 万円)
* ----------------
*/
CREATE TABLE product
(
p_num INTEGER PRIMARY KEY,
p_name VARCHAR(40) NOT NULL,
type VARCHAR(20) NOT NULL,
price INTEGER NOT NULL
);
INSERT INTO product_f VALUES (101, 'Accort', 'sedan', 230);
INSERT INTO product_f VALUES (102, 'Accort Wagon', 'RV', 280);
INSERT INTO product_f VALUES (103, 'Insphire', 'sedan', 300);
INSERT INTO product_f VALUES (104, 'Hodyssey', 'RV', 280);
INSERT INTO product_f VALUES (105, 'Shtep Wagon', 'RV', 200);
▼データベースの指定

