⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaselogreportui.java

📁 这是一款应用程序与数据库联接
💻 JAVA
字号:
/* ===========================================================
 * JDBMonitor : a flexiable JDBC Monitor for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2006-2006, by yang zhongke
 *
 * Project Info:  http://www.cownew.com
 *
 * 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 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.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * DataBaseLogReportUI.java
 * ---------------
 * (C) Copyright 2006-2006, by yang zhongke
 *
 * Original Author:  yang zhongke;
 *
 * Changes
 * -------
 *
 */
package com.cownew.JDBMonitor.listenerImpl.dataBaseListener;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.cownew.JDBMonitor.common.CommonUtils;
import com.cownew.JDBMonitor.common.InstanceUtils;
import com.cownew.JDBMonitor.common.SQLTypeEnum;
import com.cownew.JDBMonitor.listenerImpl.uicommon.SwingUtils;

/**
 * Main Frame of the DataBaseLog viewer
 * @author yang zhongke
 */
public class DataBaseLogReportUI extends AbstractDataBaseLogReportUI
{
	private static final long serialVersionUID = -4567895895050250362L;
	
	protected Connection conn;
	
	protected LogFilterUI filterUI;
	
	protected RptConnectConfigUI connectUI;
	
	public DataBaseLogReportUI() throws HeadlessException
	{
		super();
	}
	
	protected void menuItemFilter_actionPerformed(ActionEvent e)
	{
		if (filterUI == null)
		{
			filterUI = new LogFilterUI(this);
			filterUI.setSize(600, 150);
			filterUI.setModal(true);
			filterUI.setResizable(false);
			filterUI.setTitle("Filter");
		}		
		filterUI.show();
		if (!filterUI.isOK())
		{
			return;
		}
		StringBuffer sql = new StringBuffer();
		List paramList = new ArrayList();
		sql.append("select FId,FBeginTime,FEndTime,FSQL,FSQLType,FParameters from T_Log_SQLLog\n");
		sql.append("where 1=1\n");
		if (filterUI.getBeginTimeBegin() != null)
		{
			sql.append("and FBeginTime>=?\n");
			paramList.add(filterUI.getBeginTimeBegin());
		}
		if (filterUI.getBeginTimeEnd() != null)
		{
			sql.append("and FBeginTime<=?\n");
			paramList.add(filterUI.getBeginTimeEnd());
		}
		if (!CommonUtils.isEmptyString(filterUI.getSQLLike()))
		{
			sql.append("and FSQL like ?\n");
			paramList.add(filterUI.getSQLLike());
		}

		Vector vector = filterUI.getSQLTypeList();
		if (vector.size() > 0)
		{
			sql.append("and (");
			for (int i = 0, n = vector.size(); i < n; i++)
			{
				if (i > 0)
				{
					sql.append(" or ");
				}
				SQLTypeEnum type = (SQLTypeEnum) vector.get(i);
				sql.append(" FSQLType=").append(type.getValue());
			}
			sql.append(")\n");
			
		}
		sql.append("order by FBeginTime ASC");
		try
		{
			executeQuery(sql.toString(), paramList);
		} catch (SQLException se)
		{
			se.printStackTrace();
			JOptionPane.showMessageDialog(this, se.toString());
		}
	}

	protected void menuItemConnect_actionPerformed(ActionEvent e)
	{
		showConnectDialog();
		
	}

	private boolean showConnectDialog()
	{
		if(connectUI==null)
		{
			connectUI = new RptConnectConfigUI(this);
			connectUI.setSize(400,150);
			connectUI.setModal(true);
			connectUI.setResizable(false);
			connectUI.setTitle("Connect");
		}		
		connectUI.show();
		if(!connectUI.isOK())
		{
			return false;
		}
		
		try
		{
			if(conn!=null)
			{
				InstanceUtils.closeConnection(conn);
			}
			Class.forName(connectUI.getJDBCDriver());
			conn = DriverManager.getConnection(connectUI.getDBURL());
			menuItemFilter.setEnabled(true);
		} catch (Exception ex)
		{
			JOptionPane.showMessageDialog(this,ex.toString());
		}
		return true;
	}

	protected void menuItemTimeSeries_actionPerformed(ActionEvent e)
	{
		JOptionPane.showMessageDialog(this,"TODO");
	}

	private void executeQuery(String sql, List paramList) throws SQLException
	{		
		PreparedStatement ps = null;
		ResultSet rs = null;

		try
		{				
			ps = conn.prepareStatement(sql);
			for (int i = 0, n = paramList.size(); i < n; i++)
			{
				ps.setObject(i + 1, paramList.get(i));
			}
			rs = ps.executeQuery();
			DefaultTableModel tableModel = new DefaultTableModel(tableHead,0);
		
			while(rs.next())
			{
				Timestamp beginTime = rs.getTimestamp("FBeginTime");
				Timestamp endTime = rs.getTimestamp("FEndTime");
				String sqlStmt = rs.getString("FSQL");
				int sqlType = rs.getInt("FSQLType");								
				String parameter = rs.getString("FParameters");
				
				tableModel.addRow(new Object[]{beginTime,
						endTime,sqlStmt,new Integer(sqlType),
						parameter});
			}
			tableLogDetail.setModel(tableModel);
		} finally
		{
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			
		}
	}

	public static void main(String[] args)
	{
		DataBaseLogReportUI ui = new DataBaseLogReportUI();
		SwingUtils.initWindowSize(ui);	
		ui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		ui.setTitle("Database SQL Log Report");
		ui.show();
	}
	
	protected void onOpend(WindowEvent e)
	{
		super.onOpend(e);
		if(showConnectDialog()==false)
		{
			dispose();
		}
	}

	protected void onClosed(WindowEvent e)
	{
		super.onClosed(e);
		InstanceUtils.closeConnection(conn);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -