001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.plugin.query; 017 018import java.sql.PreparedStatement; 019import java.sql.ResultSet; 020import java.sql.SQLException; 021 022import org.opengion.fukurou.util.Closer; 023import org.opengion.fukurou.util.ErrorMessage; 024import org.opengion.fukurou.util.StringUtil; 025import org.opengion.hayabusa.common.HybsSystem; 026import org.opengion.hayabusa.common.HybsSystemException; 027import org.opengion.hayabusa.db.AbstractQuery; 028 029/** 030 * 引数引き当て(PreparedStatement) を利用した登録系Queryです。 031 * 032 * java.sql.PreparedStatement を用いて、データベース検索処理を行います。 033 * 引数に、指定した値を配列で渡します。 034 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している 035 * ため,ここでは、execute() メソッドを実装しています。 036 * このクラスでは、ステートメント文を execute() する事により,データベースを 037 * 検索した結果を DBTableModel に割り当てます。 038 * 039 * ※postgres8.3以降では、数値型の列に対して、実行した場合は、型相違のエラーが発生します。 040 * postgres8.3以降で利用する場合は、postgres側に暗黙の型変換(CAST)の実装を検討して下さい。 041 * 042 * @og.formSample 043 * 例: 044 * 可変引数付きのSQL文を実行します。 045 * これは、INSERT,UPDATE,DELETE など、どのようなSQL文でも実行できます。 046 * names 属性で指定するのは、DBTableModelのカラム名で、その値が順番に、 047 * 引数(?記号)の個所に設定されます。 048 * 選択されたデータ(行)の数だけ、繰り返し実行されます。 049 * 050 * jsp/TYPE1A/copy.jsp 051 * <og:value scope="session" 052 * key="names" 053 * value="CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG" /> 054 * <og:value scope="session" key="SQL" > 055 * INSERT INTO GEA08 056 * (CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG, 057 * FGJ,DYSET,DYUPD,USRSET,USRUPD,PGUPD) 058 * VALUES 059 * (?,?,?,?,?,?, 060 * '1','{@USER.YMDH}','{@USER.YMDH}','{@USER.ID}','{@USER.ID}','{@GUI.KEY}') 061 * </og:value> 062 * 063 * jsp/TYPE1A/entry.jsp 064 * lt;h:update 065 * command = "{@command}" 066 * queryType = "JDBCPrepared" 067 * names = "{@names}" > 068 * {@SQL} 069 * </og:update> 070 * 071 * <!-- 前画面で指定のSQL文を削除します。(scope="session"なので削除が必要。) --> 072 * <og:value scope="session" key="names" command="REMOVE" /> 073 * <og:value scope="session" key="SQL" command="REMOVE" /> 074 * 075 * 以下はSELECTで使用する場合の例 076 * ※値はnamesに指定した値が、?に順番に設定されます。 077 * (andタグのplaceHolder属性は、valueに指定した式を、実行する or 実行しないの判定に利用されます。) 078 * <og:query command="NEW" queryType="JDBCPrepared" names="NO,NAME,KBN"> 079 * SELECT 080 * NO,NAME,KBN 081 * FROM 082 * T01 083 * <og:where> 084 * <og:and value="NO = ?" placeHolder="{@NO}" /> 085 * <og:and value="NAME LIKE ? || '%'" placeHolder="{@NAME}" /> 086 * <og:and value="KBN IN (?)" multi="true" placeHolder="{@KBN}"/> 087 * </og:where> 088 * </og:query> 089 * 090 * @og.rev 4.0.0.0 (2005/01/31) 廃止する方向です。(Query_JDBCTableUpdate.javaへ) 091 * @og.group データ表示 092 * @og.group データ編集 093 * 094 * @deprecated 4.0.0 (2005/01/31) 廃止。Query_JDBCTableUpdate.java を使用する方向で検討願います。 095 * @version 4.0 096 * @author Kazuhiko Hasegawa 097 * @since JDK5.0, 098 */ 099@Deprecated 100public class Query_JDBCPrepared extends AbstractQuery { 101 //* このプログラムのVERSION文字列を設定します。 {@value} */ 102 private static final String VERSION = "5.3.8.0 (2011/08/01)"; 103 104 private PreparedStatement pstmt = null; 105 106 /** 107 * execute()が実行された場合は、execute(null)の処理を行う。 108 * 109 * @og.rev 5.10.2.1 (2018/08/18) 新規追加。 110 **/ 111 public void execute() { 112 execute(null); 113 } 114 115 /** 116 * 引数配列付のクエリーを実行します。 117 * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。 118 * これは、PreparedQuery で使用する引数を配列でセットするものです。 119 * select * from emp where deptno = ? and job = ? などの PreparedQuery の 120 * ? 部分の引数を 121 * 順番にセットしていきます。 122 * 123 * @og.rev 2.1.2.3 (2002/12/02) データベース更新時に、更新フラグをセットするように変更 124 * @og.rev 2.3.1.3 (2003/01/28) Open Cursor が、大量に残る件の対応。ResultSet を close() 125 * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。 126 * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。 127 * @og.rev 3.5.6.0 (2004/06/18) PreparedStatement をexecute 間で使いまわします。 128 * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。 129 * @og.rev 5.3.8.0 (2011/08/01) pstmt.setObject で、useParamMetaData の判定を避けるため、pstmt.setString で代用(PostgreSQL対応) 130 * 131 * @param args オブジェクトの引数配列 132 */ 133 @Override 134 public void execute(final String[] args) { 135 ResultSet resultSet = null ; 136 try { 137 if( pstmt== null ) { 138 pstmt = getConnection().prepareStatement( getStatement() ); 139 pstmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT ); 140 } 141 142 if( args != null ) { 143 for( int i=0; i<args.length; i++ ) { 144// pstmt.setObject( i+1,StringUtil.rTrim( args[i] ) ); // 5.3.8.0 (2011/08/01) 処理の簡素化 145 pstmt.setString( i+1,StringUtil.rTrim( args[i] ) ); // 5.3.8.0 (2011/08/01) 処理の簡素化 146 } 147 } 148 149 boolean status = pstmt.execute(); 150 if( status ) { 151 resultSet = pstmt.getResultSet(); 152 createTableModel( resultSet ); 153 setUpdateFlag( false ); 154 } 155 else { 156 setExecuteCount( pstmt.getUpdateCount() ); 157 } 158 159 setErrorCode( ErrorMessage.OK ); 160 } 161 catch ( SQLException ex ) { 162 setErrorCode( ErrorMessage.EXCEPTION ); 163 Closer.stmtClose( pstmt ); 164 165 String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR 166 + getStatement() + HybsSystem.CR; 167 rollback(); 168 realClose(); 169 throw new HybsSystemException( errMsg,ex ); // 3.5.5.4 (2004/04/15) 引数の並び順変更 170 } 171 finally { 172 Closer.resultClose( resultSet ); 173 } 174 } 175 176 /** 177 * PreparedStatement をクローズします。 178 */ 179 @Override 180 public void close() { 181 Closer.stmtClose(pstmt); 182 super.close(); 183 } 184}