📄 databasedblistener.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.]
*
* ---------------
* DataBaseDBListener.java
* ---------------
* (C) Copyright 2006-2006, by yang zhongke
*
* Original Author: yang zhongke;
*
* Changes
* -------
*
*/
package com.cownew.JDBMonitor.listenerImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.cownew.JDBMonitor.common.IDBListener;
import com.cownew.JDBMonitor.common.InstanceUtils;
import com.cownew.JDBMonitor.common.LoggerException;
import com.cownew.JDBMonitor.common.LoggerUtils;
import com.cownew.JDBMonitor.common.SQLInfo;
import com.cownew.JDBMonitor.listenerImpl.dataBaseListener.RandomGUID;
/**
* <p>this DataBaseDBListener will write the loginfo to database</p>
* <p>the database to be writen is defined in the "arg" attribute of the "Listener" tag</p>
* <Listener class="com.cownew.JDBMonitor.listenerImpl.DataBaseDBListener" <br />
* arg="dburl=jdbc:odbc:MQIS;user=;password=;logtable=T_Log_SQLLog"/><br/>
* @author yang zhongke
*/
public class DataBaseDBListener implements IDBListener
{
private String dbUrl ;
private String user;
private String password;
private String logTable;
private Connection dbConnection;
String insertSQL;
public static final String DEFAULTSQLLOGTABLE = "T_Log_SQLLog";
public DataBaseDBListener()
{
super();
}
public void init(String arg) throws LoggerException
{
//not dburl=(.+);?(user=.*;password=.*;)?(logtable=.+)?
//because "+" in regular express is greedy.so add ? after + to make it lazy
Pattern patAll = Pattern.compile("dburl=(.+?);?(user=.*;password=.*;)?(logtable=.+)?");
Matcher matAll = patAll.matcher(arg);
if(!matAll.matches())
{
throw new LoggerException("Invalid argument:"+arg);
}
dbUrl = matAll.group(1);
String userPwd = matAll.group(2);
String logTableAll = matAll.group(3);
if(logTableAll!=null&&logTableAll.trim().length()>0)
{
Pattern patLogTableAll = Pattern.compile("logtable=(.+)");
Matcher matLogTable = patLogTableAll.matcher(logTableAll);
matLogTable.matches();
logTable = matLogTable.group(1);
}
else
{
logTable = DEFAULTSQLLOGTABLE;
}
if (userPwd != null && userPwd.trim().length() > 0)
{
Pattern patUserPwd = Pattern.compile("user=(.*);password=(.*);");
Matcher matUserPwd = patUserPwd.matcher(userPwd);
if (!matUserPwd.matches())
{
throw new LoggerException("Invalid argument:" + arg);
}
user = matUserPwd.group(1);
password = matUserPwd.group(2);
}
try
{
if(user!=null&&user.trim().length()>0)
{
dbConnection = DriverManager.getConnection(dbUrl,user,password);
}
else
{
dbConnection = DriverManager.getConnection(dbUrl);
}
} catch (SQLException e)
{
throw LoggerUtils.toLoggerException(e);
}
StringBuffer sbSQL = new StringBuffer();
sbSQL.append("insert into ").append(logTable)
.append("(FID,FBeginTime,FEndTime,FSQL,FSQLType,FParameters)\n");
sbSQL.append("values(?,?,?,?,?,?)");
insertSQL = sbSQL.toString();
}
public void logSql(SQLInfo info) throws LoggerException
{
PreparedStatement logStatement = null;
try
{
logStatement = dbConnection.prepareStatement(insertSQL);
logStatement.setString(1,new RandomGUID().toString());
logStatement.setTimestamp(2,info.getBeginTime());
logStatement.setTimestamp(3,info.getEndTime());
logStatement.setString(4,info.getSql());
logStatement.setInt(5,info.getSqlType().getValue());
logStatement.setString(6,info.getParameters().toString());
logStatement.execute();
} catch (SQLException e)
{
throw LoggerUtils.toLoggerException(e);
}
finally
{
InstanceUtils.closeStatement(logStatement);
}
}
protected void finalize() throws Throwable
{
super.finalize();
if (dbConnection != null)
{
dbConnection.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -