package jp.co.wownet.education.jdbc; import java.lang.*; import java.util.*; import java.sql.*; /** 従業員情報のテーブルにアクセスするロジックビーンです。 @author Fumitaka Makino */ public class EmpLogicBean extends abstractLogicBean{ /** コンストラクタ @param driver ドライバクラス @param url JDBC-URL @param user ユーザー名 @param pass パスワード @exception SQLException コネクションの取得に失敗したときにスロー @exception ClassNotFoundException ドライバのロードに失敗 */ public EmpLogicBean( String driver , String url , String user , String pass ) throws SQLException,ClassNotFoundException { //スーパークラスのコンストラクタ super( driver , url , user , pass ); } /** 全てのEmpのリストを配列で取得します。 @return EmpValue[] 該当する従業員情報の配列 @exception SQLException クエリー中に何らかの問題が発生したときにスロー */ public EmpValue[] getAllList()throws SQLException{ //プリペアの宣言 PreparedStatement pst = null; //リターン用のEmpValue配列 EmpValue[] emps = null; try{ //PreparedStatementの生成 pst = super.con.prepareStatement("select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from EMP"); //ResultSetの取得 ResultSet rs = pst.executeQuery(); Vector tmpVec = new Vector(); EmpValue tmpEmp = null; while( rs.next() ){ //EmpValueを生成 tmpEmp = new EmpValue(); //各値をセット tmpEmp.setEmpno( rs.getString(1) ); tmpEmp.setEname( rs.getString(2) ); tmpEmp.setJob( rs.getString(3) ); tmpEmp.setMgr( rs.getString(4) ); tmpEmp.setHiredate( rs.getString(5) ); tmpEmp.setSal( rs.getString(6) ); tmpEmp.setComm( rs.getString(7) ); tmpEmp.setDeptno( rs.getString(8) ); //Vectorに追加 tmpVec.add( tmpEmp ); } //Vectorの配列化 emps = (EmpValue[])tmpVec.toArray( new EmpValue[0] ); }catch( SQLException e ){ throw e; }catch( Exception e ){ throw new SQLException( e.getMessage() ); }finally{ try{ //PreparedStatementのクローズ if(pst!=null) pst.close(); }catch(Exception e){ //ログ出力 e.printStackTrace(); } } //nullなら0配列化 if(emps==null) emps = new EmpValue[0]; return emps; } /** EMPNOにより1件の従業員情報を取得します。該当する従業員情報がないときには例外をスローします。 以前は該当情報がないときにはnullをリターンしていました。しかしそのようなnull判定による手法は 手軽な反面、本質的に安全ではなくデバッグの際に障害となる場合があります。 @param empno 従業員番号 @return EmpValue 該当する従業員情報 @exception EmpLogicException 該当する従業員情報がないときにスロー @exception SQLException クエリー中に何らかの問題が発生したときにスロー */ public EmpValue getEmp( int empno )throws SQLException,EmpLogicException{ //プリペアの宣言 PreparedStatement pst = null; //リターン用のEmpValue配列 EmpValue emp = null; try{ //PreparedStatementの生成 pst = super.con.prepareStatement("select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from EMP where EMPNO=?"); //従業員番号のセット pst.setInt( empno ); //ResultSetの取得 ResultSet rs = pst.executeQuery(); if( rs.next() ){ //EmpValueを生成 emp = new EmpValue(); //各値をセット emp.setEmpno( rs.getString(1) ); emp.setEname( rs.getString(2) ); emp.setJob( rs.getString(3) ); emp.setMgr( rs.getString(4) ); emp.setHiredate(rs.getString(5) ); emp.setSal( rs.getString(6) ); emp.setComm( rs.getString(7) ); emp.setDeptno( rs.getString(8) ); }else{ //該当レコードが存在しないとき throw new EmpLogicException("該当レコードが存在しません。"); } }catch( EmpLogicException e ){ throw e; }catch( SQLException e ){ throw e; }catch( Exception e ){ throw new SQLException( e.getMessage() ); }finally{ try{ //PreparedStatementのクローズ if(pst!=null) pst.close(); }catch(Exception e){ //ログ出力 e.printStackTrace(); } } return emp; } /** EMPNOにより1件の従業員情報を削除し、削除したレコードの件数をintで戻します。 @param empno 従業員番号 @return int 反映されたレコードの件数 @exception SQLException クエリー中に何らかの問題が発生したときにスロー */ public int deleteEmp( int empno )throws SQLException{ //プリペアの宣言 PreparedStatement pst = null; //リターン用の更新件数 int rint = 0; try{ //PreparedStatementの生成 pst = super.con.prepareStatement("delete from EMP where EMPNO=?"); //従業員番号のセット pst.setInt( empno ); //SQLの発行、更新件数の取得 rint = pst.executeUpdate(); }catch( SQLException e ){ throw e; }catch( Exception e ){ throw new SQLException( e.getMessage() ); }finally{ try{ //PreparedStatementのクローズ if(pst!=null) pst.close(); }catch(Exception e){ //ログ出力 e.printStackTrace(); } } return rint; } /** 最大EMPNO+1で情報をインサートします。 @param emp インサートしたい従業員情報 @return int 反映されたレコードの件数 @exception SQLException クエリー中に何らかの問題が発生したときにスロー */ public int insertEmp( EmpValue emp )throws SQLException{ //プリペアの宣言 PreparedStatement pst = null; //リターン用の更新件数 int rint = 0; try{ //PreparedStatementの生成 pst = super.con.prepareStatement("insert into EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) select max(EMPNO)+1,?,?,?,to_date('yyyy/mm/dd',?),?,?,? from EMP"); //従業員番号のセット pst.setString( 1 , emp.getEname() ); pst.setString( 2 , emp.getJob() ); pst.setString( 3 , emp.getMgr() ); pst.setString( 4 , emp.getHiredate() );// yyyy/dd/mm pst.setString( 5 , emp.getSal() ); pst.setString( 6 , emp.getComm() ); pst.setString( 7 , emp.getDeptno() ); //SQLの発行、更新件数の取得 rint = pst.executeUpdate(); }catch( SQLException e ){ throw e; }catch( Exception e ){ throw new SQLException( e.getMessage() ); }finally{ try{ //PreparedStatementのクローズ if(pst!=null) pst.close(); }catch(Exception e){ //ログ出力 e.printStackTrace(); } } return rint; } /** 実行用メインメソッド @param args 起動時引数、無し */ public static void main( String[] args )throws SQLException,ClassNotFoundException{ //ドライバークラスの設定 String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver"; //データベースののURLの設定 String DB_URL = "jdbc:oracle:thin:@192.168.1.14:1521:orcl"; //ユーザー名の設定 String DB_USER = "scott"; //パスワードの設定 String DB_PASS = "tiger"; //ロジックビーンのインスタンスを取得 EmpLogicBean logic = new EmpLogicBean(JDBC_DRIVER,DB_URL,DB_USER,DB_PASS); //従業員情報配列の取得 EmpValue[] empArray = logic.getAllList(); //コネクションのクローズ logic.close(); //empArrayの内容を出力 for( int i=0;i