解答例 - 実習課題4 - 4.ラージオブジェクト
(実習課題4)
以下のプログラムを作成しなさい。
- 「product_introduction」に格納されているホームページを表示するウィンドウプログラム。
- 製品番号を指定したら、該当するホームページが表示されるようにする事。
解答例
▼データベースアクセス用クラスのソース
package com.techscore.jdbc.chapter4.exercise4;
/**
* ProductIntroductionDAO.java
* TECHSCORE JDBC4章 実習課題4
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.io.InputStream;
import java.io.BufferedReader;
import java.sql.ResultSet;
public class ProductIntroductionDAO{
private PreparedStatement statement = null;
private Connection conn = null;
private Connection getConnection() throws SQLException,ClassNotFoundException{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://dbserver:5432/Training"
,"postgres" //ユーザ名
,""); //パスワード
conn.setAutoCommit(false); //自動コミットモード解除
return conn;
}
public BufferedReader loadImage(int pNum) throws SQLException,ClassNotFoundException{
conn = getConnection();
BufferedReader reader = null;
InputStream input = null;
final String sql = "select introduction from product_introduction where p_num=?";
statement = conn.prepareStatement(sql);
statement.setInt(1,pNum);
ResultSet result = statement.executeQuery();
if (!result.next()) {
System.out.println("製品番号" + pNum + "のHTMLファイルはありません");
throw new SQLException();
}else{
reader=new BufferedReader(result.getCharacterStream(1));
}
conn.commit();
return reader;
}
public void finishTransaction() throws SQLException{
try{
statement.close();
}finally{
if (conn != null){
conn.close();
}
}
}
}
▼データ挿入測定クラスのソース
package com.techscore.jdbc.chapter4.exercise4;
/**
* DisplayHtmlFile.java
* TECHSCORE JDBC4章 実習課題4
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
import javax.swing.JFrame;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.sql.SQLException;
public class DisplayHtmlFile extends JFrame {
protected static final String OUTFILE = "tmpFile.html";
private static StringBuffer bf = null;
public DisplayHtmlFile(){
super("ホームページ表示");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200, 200);
JEditorPane editor = new JEditorPane();
try{
editor.setPage("http://localhost:8080/jdbc/" + OUTFILE);
}catch(IOException e){
e.printStackTrace();
}
editor.setContentType("text/html");
editor.setEditable(false);
getContentPane().add(new JScrollPane(editor));
}
public static void main(String[] args) {
try {
ProductIntroductionDAO introductionDAO = new ProductIntroductionDAO();
int pNum =Integer.parseInt(args[0]);
BufferedReader reader = introductionDAO.loadImage(pNum);
//表示用ファイルへ保存
BufferedWriter writer =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(OUTFILE), "EUC-JP"));
String line;
while((line=reader.readLine())!=null){
writer.write(line);
writer.newLine();
}
writer.close();
reader.close();
introductionDAO.finishTransaction();
//画像表示
new DisplayHtmlFile().setVisible(true);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage:java DisplayHtmlFile [製品番号]");
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
▼ディレクトリ構成例
Webアプリケーションルートをjdbcディレクトリとすると jdbcディレクトリ直下にファイルを出力するようになっています。 回答のディレクトリ構成を以下に記述します。 ├─jdbc Webアプリケーションルート │ └─tmpFile.html 表示用HTMLファイル │ └─com │ └─techscore │ └─jdbc │ └─chapter4 │ └─exercise4 DisplayHtmlFile.class │ ProductIntroductionDAO.class
▼データベースの指定

