alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
** Copyright (c) 1998 by Timothy Gerard Endres
** <mailto:time@ice.com>  
** 
** This program is free software.
** 
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included	with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE. 
** 
*/

package com.ice.sqlclient;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.ice.util.AWTUtilities;


public class
UpdatePanel extends JPanel
		implements ActionListener
	{
	private JTextArea		updateText;
	private JTextArea		resultText;
	private JButton			queryButton;

	private JPanel			updatePanel;
	private JPanel			resultPanel;
	private JSplitPane		splitPane;


	public
	UpdatePanel()
		{
		this.setDoubleBuffered( true );
		this.establishContents();
		}

	public void
	actionPerformed( ActionEvent event )
		{
		String command = event.getActionCommand();

		if ( command.equals( "UPDATE" ) )
			{
			String updText = this.updateText.getText();
			
			SQLClientHandler handler =
				SQLClientHandler.getCurrentHandler();

			if ( handler == null )
				{
				this.resultText.setText
					( "INTERNAL ERROR: current handler is null." );
				return;
				}

			if ( ! handler.isOpen() )
				{
				this.resultText.setText
					( "INTERNAL ERROR: connection is not open." );
				return;
				}

			try {
				int totalStatements = 0;
				int totalRows = 0;

				for ( int off = 0 ; updText != null ; )
					{
					int index = updText.indexOf( ";\n", off );

					String stStr = null;
					if ( index >= 0 )
						{
						stStr = updText.substring( 0, index );
						updText = updText.substring( index + 2 );
						}
					else
						{
						stStr = updText;
						updText = null;
						}

					if ( stStr.endsWith( ";" ) )
						stStr = stStr.substring
								( 0, (stStr.length() - 1) );
					
					if ( stStr.length() > 0 )
						{
						totalStatements++;

						totalRows +=
							handler.performSQLUpdate
								( stStr + "\n" );
						}
					}
				
				this.resultText.setText
					( "" + totalStatements + " statement"
						+ ( totalStatements > 1 ? "s" : "" )
						+ ".\n"
						+ totalRows + " row"
						+ ( totalRows > 1 ? "s" : "" )
						+ " affected." );
				}
			catch ( SQLException ex )
				{
				this.resultText.setText
					( "SQLException: performing update\n"
						+ ex.getMessage() );
				}
			}
		else
			{
			this.resultText.setText
				( "INTERNAL ERROR: unknown command: " + command );
			}
		}

	private void
	establishContents()
		{
		JLabel	lbl;
		int		row = 0;
		JScrollPane scroller;

		this.setLayout( new BorderLayout() );

		this.updatePanel = new JPanel();
		this.updatePanel.setLayout( new GridBagLayout() );
		this.updatePanel.setPreferredSize( new Dimension(450, 125) );

		lbl = new JLabel( "Update:" );
		AWTUtilities.constrain(
			this.updatePanel, lbl,
			GridBagConstraints.NONE,
			GridBagConstraints.WEST,
			0, row, 2, 1, 0.0, 0.0 );

		++row;

		this.updateText = new JTextArea();
		scroller = new JScrollPane();
		scroller.getViewport().add( this.updateText );
		AWTUtilities.constrain(
			this.updatePanel, scroller,
			GridBagConstraints.BOTH,
			GridBagConstraints.CENTER,
			0, row, 2, 1, 1.0, 1.0 );

		++row;
		
		this.queryButton =
			new JButton( "Perform Update" );
		this.queryButton.setActionCommand( "UPDATE" );
		this.queryButton.addActionListener( this );
		AWTUtilities.constrain(
			this.updatePanel, this.queryButton,
			GridBagConstraints.HORIZONTAL,
			GridBagConstraints.CENTER,
			0, row, 2, 1, 1.0, 0.0 );
		
		this.resultPanel = new JPanel();
		this.resultPanel.setLayout( new GridBagLayout() );

		row = 0;

		lbl = new JLabel( "Result:" );
		AWTUtilities.constrain(
			this.resultPanel, lbl,
			GridBagConstraints.NONE,
			GridBagConstraints.WEST,
			0, row, 1, 1, 0.0, 0.0 );

		++row;

		this.resultText = new JTextArea();

		scroller = new JScrollPane();
		scroller.getViewport().add( this.resultText );
		AWTUtilities.constrain(
			this.resultPanel, scroller,
			GridBagConstraints.BOTH,
			GridBagConstraints.CENTER,
			0, row, 1, 1, 1.0, 1.0 );

		++row;

		
		this.splitPane =
			new JSplitPane
				( JSplitPane.VERTICAL_SPLIT,
					this.updatePanel, this.resultPanel );

		this.splitPane.setDividerSize( 3 );
		this.splitPane.setContinuousLayout( true );

		this.add( this.splitPane, BorderLayout.CENTER );
		}

	}


... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.