org.kawanfw.test.run.callable.CallableStatementTest.java Source code

Java tutorial

Introduction

Here is the source code for org.kawanfw.test.run.callable.CallableStatementTest.java

Source

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.test.run.callable;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.lang3.SystemUtils;
import org.junit.Assert;
import org.junit.Test;
import org.kawanfw.sql.api.util.SqlUtil;
import org.kawanfw.test.parms.ConnectionLoader;
import org.kawanfw.test.parms.SqlTestParms;
import org.kawanfw.test.util.MessageDisplayer;

public class CallableStatementTest {
    @Test
    public void test() throws Exception {
        Connection connection = null;
        try {
            SqlTestParms.ACEQL_URL_2 = SqlTestParms.ACEQL_URL_TOMCAT_EMBEDED_LOCALHOST_SSL_2;
            connection = ConnectionLoader.getAceqlConnection2();
            test(connection);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {

        if (SystemUtils.IS_JAVA_1_7) {
            System.setProperty("java.net.preferIPv4Stack", "true");
        }

        new CallableStatementTest().test();
    }

    /**
     * @param connection
     *            the AceQL Connection
     * 
     * @throws SQLException
     * @throws Exception
     */
    public void test(Connection connection) throws SQLException, Exception {

        MessageDisplayer.initClassDisplay(this.getClass().getSimpleName());

        SqlUtil sqlUtil = new SqlUtil(connection);
        if (sqlUtil.isDB2()) {
            MessageDisplayer.display("No callableStatementTest with DB2");
            return;
        }

        if (sqlUtil.isHSQLDB()) {
            MessageDisplayer.display("No callableStatementTest with HSQLDB");
            return;
        }

        if (sqlUtil.isInformix()) {
            MessageDisplayer.display("No callableStatementTest with Informix");
            return;
        }

        if (sqlUtil.isSQLServer()) {
            MessageDisplayer.display("No callableStatementTest with SQL Server");
            return;
        }

        // Now test callableStatement
        callableStatementTest(connection);

    }

    public static void callableStatementTest(Connection connection) throws Exception {
        String testString = "to be capitalized";

        CallableStatement upperProc = connection.prepareCall("{ ? = call upper( ? ) }");
        upperProc.registerOutParameter(1, Types.VARCHAR);
        upperProc.setString(2, testString);
        boolean isResultSet = upperProc.execute();

        System.out.println("isResultSet: " + isResultSet);
        String upperCased = upperProc.getString(1);

        String stringparm = upperProc.getString(2);
        System.out.println("stringparm: " + stringparm);

        upperProc.close();

        Assert.assertEquals(testString.toUpperCase(), upperCased);

        System.out.println("test string: " + testString);
        System.out.println("result: " + upperCased);
    }
}