All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.example.jdbc.Bookstore Maven / Gradle / Ivy

Go to download

Mockrunner is a lightweight framework for unit testing applications in the J2EE environment. It supports servlets, filters, tag classes and Struts actions. It includes a JDBC a JMS and a JCA test framework and can be used to test EJB based applications.

The newest version!
package com.mockrunner.example.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * This example simulates the order of some books. It iterates through
 * a List of ISBN numbers. If the current quantity is at least
 * one, it reduces the quantity and adds the corresponding ISBN number to
 * the result List.
 * 
 * This example uses one table books with at least the columns
 * isbn and quantity.
 */
public class Bookstore
{
    public static List order(Connection connection, List isbnNumbers) throws SQLException
    {
        ArrayList resultList = new ArrayList();
        Statement statement = null;
        ResultSet result = null;
        try
        {
            connection.setAutoCommit(false);
            StringBuffer query = new StringBuffer("select isbn, quantity from books where (");
            for(int ii = 0; ii < isbnNumbers.size(); ii++)
            {
                query.append("isbn='" + isbnNumbers.get(ii)+ "'");
                if(ii < isbnNumbers.size() - 1)
                {
                    query.append(" or ");
                }
            }
            query.append(")");
            statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
            result = statement.executeQuery(query.toString());
            while(result.next())
            {
                int quantity = result.getInt("quantity");
                if(quantity > 0)
                {
                    result.updateInt("quantity", quantity - 1);
                    result.updateRow();
                    resultList.add(result.getString("isbn"));
                }
            }
            connection.commit();
        }
        catch(Exception exc)
        {
            connection.rollback();
        }
        if(null != result) result.close();
        if(null != statement) statement.close();
        return resultList;
    }  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy