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 */
016 package org.opengion.plugin.query;
017
018 import org.opengion.hayabusa.common.HybsSystem;
019 import org.opengion.hayabusa.common.HybsSystemException;
020 import org.opengion.hayabusa.db.AbstractQuery;
021 import org.opengion.fukurou.util.ErrorMessage;
022 import org.opengion.fukurou.util.StringUtil;
023 import org.opengion.fukurou.util.Closer;
024
025 import java.sql.PreparedStatement;
026 import java.sql.ResultSet;
027 import java.sql.SQLException;
028
029 /**
030 * 引数引き当て(PreparedStatement) を利用した登録系Queryですã?
031 *
032 * java.sql.PreparedStatement を用ã�?�¦、データベã?ス検索処ç�?‚’行いますã?
033 * 引数に、指定したå?をé?列で渡しますã?
034 * å†?ƒ¨変数の受け渡しã?ãƒ?ƒ•ォルト実è£??、AbstractQuery クラスを継承してã�?‚‹
035 * ため,ここでは、execute() メソãƒ?ƒ‰を実è£?�—てã�?�¾すã?
036 * こã?クラスでは、スãƒ??トメント文ã‚?execute() する事により,ãƒ??タベã?スã‚?
037 * 検索した結果ã‚?DBTableModel に割り当てますã?
038 *
039 * @og.formSample
040 * 例ï¼?
041 * 可変引数付きの?³?±?¬æ–?‚’実行しますã?
042 * これは、INSERT,UPDATE,DELETE など、どのようなSQLæ–?�§も実行できますã?
043 * names 属æ?でæŒ?®šするã?は、DBTableModelのカラãƒ?��で、その値がé?番にã€?
044 * 引数(?記号)の個所に設定されますã?
045 * 選択されたãƒ??タ(è¡?の数ã�?�‘、繰り返し実行されますã?
046 *
047 * jsp/TYPE1A/copy.jsp
048 * <og:value scope="session"
049 * key="names"
050 * value="CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG" />
051 * <og:value scope="session" key="SQL" >
052 * INSERT INTO GEA08
053 * (CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG,
054 * FGJ,DYSET,DYUPD,USRSET,USRUPD,PGUPD)
055 * VALUES
056 * (?,?,?,?,?,?,
057 * '1','{@USER.YMDH}','{@USER.YMDH}','{@USER.ID}','{@USER.ID}','{@GUI.KEY}')
058 * </og:value>
059 *
060 * jsp/TYPE1A/entry.jsp
061 * lt;h:update
062 * command = "{@command}"
063 * queryType = "JDBCPrepared"
064 * names = "{@names}" >
065 * {@SQL}
066 * </og:update>
067 *
068 * <!-- 前画面でæŒ?®šã?SQLæ–?‚’削除しますã?(scope="session"なので削除がå¿?¦�ã?) -->
069 * <og:value scope="session" key="names" command="REMOVE" />
070 * <og:value scope="session" key="SQL" command="REMOVE" />
071 *
072 * @og.rev 4.0.0.0 (2005/01/31) å»?¢する方向ですã?(Query_JDBCTableUpdate.javaへ)
073 * @og.group ãƒ??タ表示
074 * @og.group ãƒ??タ編é›?
075 *
076 * @deprecated 4.0.0 (2005/01/31) å»?¢。Query_JDBCTableUpdate.java を使用する方向で検討願いますã?
077 * @version 4.0
078 * @author Kazuhiko Hasegawa
079 * @since JDK5.0,
080 */
081 @Deprecated public class Query_JDBCPrepared extends AbstractQuery {
082 //* こã?プログラãƒ??VERSIONæ–?—å?を設定しますã? {@value} */
083 private static final String VERSION = "5.3.8.0 (2011/08/01)" ;
084
085 private PreparedStatement pstmt = null ;
086
087 /**
088 * 引数配å?付ã?クエリーを実行しますã?
089 * 処ç�??体ã?, #execute() と同様に、各サブクラスの実è£?�«依存しますã?
090 * これは、PreparedQuery で使用する引数をé?列でセãƒ?ƒˆするもã?ですã?
091 * select * from emp where deptno = ? and job = ? などの PreparedQuery の
092 * ? 部åˆ??引数ã‚?
093 * é ?•ªにセãƒ?ƒˆしてã�?��ますã?
094 *
095 * @og.rev 2.1.2.3 (2002/12/02) ãƒ??タベã?ス更新時に、更新フラグをセãƒ?ƒˆするように変更
096 * @og.rev 2.3.1.3 (2003/01/28) Open Cursor がã?大量に残る件の対応ã?ResultSet ã‚?close()
097 * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
098 * @og.rev 3.3.3.1 (2003/07/18) ?¤?¢登録時ã?後ろスペã?スを削除するã€?
099 * @og.rev 3.5.6.0 (2004/06/18) PreparedStatement をexecute 間で使ã�?�¾わしますã?
100 * @og.rev 3.8.0.8 (2005/10/03) エラーメãƒ?‚»ージの出力é?をメãƒ?‚»ージ?‹Queryに変更しますã?
101 * @og.rev 5.3.8.0 (2011/08/01) pstmt.setObject で、useParamMetaData の判定を避けるため、pstmt.setString で代用(PostgreSQL対å¿?
102 *
103 * @param args オブジェクトã?引数配å?
104 */
105 @Override
106 public void execute( final String[] args ) {
107 ResultSet resultSet = null ;
108 try {
109 if( pstmt== null ) {
110 pstmt = getConnection().prepareStatement( getStatement() );
111 pstmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
112 }
113
114 if( args != null ) {
115 for( int i=0; i<args.length; i++ ) {
116 // pstmt.setObject( i+1,StringUtil.rTrim( args[i] ) ); // 5.3.8.0 (2011/08/01) 処ç�??簡ç´?Œ–
117 pstmt.setString( i+1,StringUtil.rTrim( args[i] ) ); // 5.3.8.0 (2011/08/01) 処ç�??簡ç´?Œ–
118 }
119 }
120
121 boolean status = pstmt.execute();
122 if( status ) {
123 resultSet = pstmt.getResultSet();
124 createTableModel( resultSet );
125 setUpdateFlag( false );
126 }
127 else {
128 setExecuteCount( pstmt.getUpdateCount() );
129 }
130
131 setErrorCode( ErrorMessage.OK );
132 }
133 catch ( SQLException ex ) {
134 setErrorCode( ErrorMessage.EXCEPTION );
135 Closer.stmtClose( pstmt );
136
137 String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
138 + getStatement() + HybsSystem.CR;
139 rollback();
140 realClose();
141 throw new HybsSystemException( errMsg,ex ); // 3.5.5.4 (2004/04/15) 引数の並びé ?¤‰更
142 }
143 finally {
144 Closer.resultClose( resultSet );
145 }
146 }
147
148 /**
149 * PreparedStatement をクローズしますã?
150 */
151 @Override
152 public void close() {
153 Closer.stmtClose( pstmt );
154 super.close();
155 }
156 }