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 java.sql.ResultSet;
019    import java.sql.SQLException;
020    import java.sql.Statement;
021    
022    import org.opengion.fukurou.util.Closer;
023    import org.opengion.fukurou.util.ErrorMessage;
024    import org.opengion.hayabusa.common.HybsSystem;
025    import org.opengion.hayabusa.common.HybsSystemException;
026    import org.opengion.hayabusa.db.AbstractQuery;
027    
028    /**
029     * æŒ?®šã?SQLæ–?‚’実行して、検索する Query クラスですã?
030     *
031     * java.sql.Statement を用ã�?�¦、データベã?ス検索処ç�?‚’行いますã?
032     * 引数は無しですã?(与えられたSQLæ–?‚’実行しますã?)
033     * å†?ƒ¨変数の受け渡しã?ãƒ?ƒ•ォルト実è£??、AbstractQuery クラスを継承してã�?‚‹
034     * ため,ここでは、execute() メソãƒ?ƒ‰を実è£?�—てã�?�¾すã?
035     * こã?クラスでは、スãƒ??トメント文ã‚?execute() する事により,ãƒ??タベã?スã‚?
036     * 検索した結果ã‚?DBTableModel に割り当てますã?
037     *
038     * @og.formSample
039     * <og:query command="{@command}" debug="false">
040     *     <!-- 先é?のカラãƒ?��がã?"WRITABLE" の場合ã?'true' or '1' で、書き込み許可が与えã‚?'2' でチェãƒ?‚¯済みになりますã?-->
041     *         select KBSAKU AS WRITABLE,
042     *                 CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG,FGJ
043     *         from GE41
044     *     <og:where>
045     *         <og:and value = "FGJ         in  ('0','1')"         />
046     *         <og:and value = "SYSTEM_ID   like '{@SYSTEM_ID}%'"  />
047     *         <og:and value = "LANG        like '{@LANG}%'"       />
048     *         <og:and value = "CLM         like '{@CLM}%'"        />
049     *         <og:and value = "KBSAKU      =    '{@KBSAKU}'"      />
050     *     </og:where>
051     *     <og:appear startKey = "order by" value = "{@ORDER_BY}"
052     *                 defaultVal = "SYSTEM_ID,CLM,LANG" />
053     * </og:query>
054     *
055     * @og.group ãƒ??タ表示
056     * @og.group ãƒ??タ編é›?
057     *
058     * @version  4.0
059     * @author   Kazuhiko Hasegawa
060     * @since    JDK5.0,
061     */
062    public class Query_JDBC extends AbstractQuery {
063            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
064            private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
065    
066            /**
067             * クエリーを実行しますã?
068             * セãƒ?ƒˆされてã�?‚‹スãƒ??トメント文字å?とそã?タイプが合ってã�?�ªã�??合ã?,
069             * エラーになりますã?
070             * 実行結果は、DBTableModel にセãƒ?ƒˆされますã?
071             *
072             * @og.rev 3.8.0.8 (2005/10/03) エラーメãƒ?‚»ージの出力é?をメãƒ?‚»ージ?‹Queryに変更しますã?
073             *
074             */
075            @Override
076            public void execute() {
077                    Statement stmt = null ;
078                    ResultSet resultSet = null ;
079                    try {
080                            stmt = getConnection().createStatement();
081                            stmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
082    
083                            boolean status = stmt.execute( getStatement() );
084    
085                            if( status ) {
086                                    resultSet = stmt.getResultSet();
087                                    createTableModel( resultSet );
088                                    setUpdateFlag( false );
089                            }
090                            else {
091                                    setExecuteCount( stmt.getUpdateCount() );
092                            }
093                            setErrorCode( ErrorMessage.OK );
094                    }
095                    catch ( SQLException ex ) {
096                            setErrorCode( ErrorMessage.EXCEPTION );
097                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
098                                                    + getStatement() + HybsSystem.CR;
099                            rollback();
100                            realClose();
101                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並びé ?¤‰更
102                    }
103                    finally {
104                            Closer.resultClose( resultSet );
105                            Closer.stmtClose( stmt );
106                    }
107            }
108    }