📄 querydialog.java
字号:
/**
* ========================================================================================
* JFreeReport Designer : a graphical designer for JFreeReport - a free Java report library
* ========================================================================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner (taquera@sherito.org);
*
* (C) Copyright 2003, by Heiko Evermann (heiko@evermann.de) and Contributors.
*
* This library 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.
*
* This library is distributed in the hope t * 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., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
package org.jfree.designer.db;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.jfree.report.modules.gui.base.components.ActionButton;
/**
* @author hev
* <p/>
* TM: Reorganized the way how DB connecions are created. a connection is now
* opened once for every selection and can be reused as long as the selected
* connection definition does not change.
* <p/>
* All DB releated stuff is managed by the DatabaseManager. Encapsulation is what
* made ObjectOrientation superior to Basic !
*/
public final class QueryDialog
extends JDialog
{
private final class ExecuteQueryAction
extends AbstractAction
{
/**
* Defines an <code>Action</code> object with a default description string and default
* icon.
*/
public ExecuteQueryAction ()
{
putValue(Action.NAME, "Execute Query");
}
/**
* Invoked when an action occurs.
*/
public final void actionPerformed (final ActionEvent e)
{
onBtExecuteQuery();
}
}
private final class CloseAction
extends AbstractAction
{
/**
* Defines an <code>Action</code> object with a default description string and default
* icon.
*/
public CloseAction ()
{
putValue(Action.NAME, "Close");
}
/**
* Invoked when an action occurs.
*/
public final void actionPerformed (final ActionEvent e)
{
onBtClose();
}
}
private JComboBox m_cbConnection;
private JTextArea m_editQuery;
private DatabaseManager manager;
public QueryDialog (final DatabaseManager manager)
{
if (manager == null)
{
throw new NullPointerException("DataBaseManager must not be null.");
}
this.manager = manager;
setModal(true);
initDialog();
}
private void initDialog ()
{
final JPanel upperPanel = new JPanel();
final BorderLayout layoutUpperPanel = new BorderLayout();
layoutUpperPanel.setVgap(5);
upperPanel.setLayout(layoutUpperPanel);
m_cbConnection = new JComboBox();
m_editQuery = new JTextArea(10, 40);
upperPanel.add(m_cbConnection, BorderLayout.NORTH);
upperPanel.add(m_editQuery, BorderLayout.CENTER);
upperPanel.setBorder(BorderFactory.createEmptyBorder(10, //top
10, //left
10, //bottom
10) //right
);
// the buttons
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2, 5, 5));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
buttonPanel.add(new ActionButton(new ExecuteQueryAction()));
buttonPanel.add(new ActionButton(new CloseAction()));
final JPanel pane = new JPanel();
pane.setLayout(new BorderLayout(0, 5));
// border layout does not really like it if north and south are both set ..
pane.add(upperPanel, BorderLayout.CENTER);
pane.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(pane);
// fill the connection combo
final Enumeration enum = manager.getDBConnections();
while (enum.hasMoreElements())
{
m_cbConnection.addItem(enum.nextElement());
}
setTitle("Edit report query");
pack();
}
private void onBtExecuteQuery ()
{
if (-1 == m_cbConnection.getSelectedIndex())
{
JOptionPane.showMessageDialog(this,
"Please choose a JDBC connection first!",
"Error", JOptionPane.WARNING_MESSAGE);
return;
}
final ConnectionEntry ce = (ConnectionEntry) m_cbConnection.getSelectedItem();
try
{
manager.setCurrentEntry(ce);
final Connection conn = manager.getConnection();
final PreparedStatement stmt = conn.prepareStatement(m_editQuery.getText());
final ResultSet rs = stmt.executeQuery();
final SerializedTableModel rstm = new SerializedTableModel(rs);
for (int i = 0; i < rstm.getColumnCount(); ++i)
{
final String sColumnName = rstm.getColumnName(i);
final String sColumnType = rstm.getColumnClass(i).getName();
System.out.println("Col " + i + ": Name=" + sColumnName + ", Type=" + sColumnType);
}
manager.setLastQueryResult(rstm);
final int numRows = rstm.getRowCount();
JOptionPane.showMessageDialog(this,
numRows + " rows read",
"Success", JOptionPane.INFORMATION_MESSAGE);
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(this,
"SQL problems:\n" + ex.getMessage() + "\n" + ce.getUrl(),
"Error", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this,
"General problem:\n" + ex.getMessage() + "\n\nPlease check datasource name",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void onBtClose ()
{
setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -