org.kawanfw.test.api.client.SelectInTransationTest.java Source code

Java tutorial

Introduction

Here is the source code for org.kawanfw.test.api.client.SelectInTransationTest.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.api.client;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

public class SelectInTransationTest {

    @Test
    public void test() throws Exception {
        Connection connection = null;
        try {
            connection = ConnectionLoader.getAceqlConnection();
            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");
        }

        SqlTestParms.REMOTE = true;
        new SelectInTransationTest().test();
    }

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

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

        if (connection instanceof RemoteConnection) {
            RemoteConnection connectionHttp = (RemoteConnection) connection;
            if (connectionHttp.isStatelessMode()) {
                MessageDisplayer.display("Select in transaction are not supported in stateless mode");
                return;
            }
        }

        DatabaseMetaData databaseMetaData = connection.getMetaData();
        MessageDisplayer.display(
                "databaseMetaData.getDatabaseProductName()   : " + databaseMetaData.getDatabaseProductName());

        // We can now use our Remote JDBC Connection as a regular Connection!
        connection.setAutoCommit(false);

        String sql = null;

        try {

            sql = "update customer set lname = ? where customer_id = ?";
            PreparedStatement prepStatement2 = connection.prepareStatement(sql);
            prepStatement2.setString(1, "Newname_1");
            prepStatement2.setInt(2, 1);
            int rc = prepStatement2.executeUpdate();
            MessageDisplayer.display("");
            MessageDisplayer.display("rc: " + rc);

            sql = "select customer_id, lname from customer where customer_id >= ?";
            PreparedStatement prepStatement = connection.prepareStatement(sql);
            prepStatement.setInt(1, 1);
            ResultSet rs = prepStatement.executeQuery();

            while (rs.next()) {
                int customerId = rs.getInt("customer_id");
                String lname = rs.getString("lname");
                System.out.println("customer_id: " + customerId);
                System.out.println("lname      : " + lname);
            }

        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.setAutoCommit(true);
        }

        MessageDisplayer.display("Done!");

    }

}