📄 jdbccnn.java
字号:
package com.icbcsdc.ddlexp.pub.connectionpool;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Properties;
import java.util.StringTokenizer;
import com.icbcsdc.ddlexp.pub.staticLog.Logger;
import com.icbcsdc.ddlexp.pub.util.ExceptionHandle;
//import com.icbc.ct.pub.log.//;
/**
* @author zhangyc
* jdbc连接类,
*/
public class JDBCCnn{
/**
* 连接标示
* */
//int id=0;
/**
* 用于标识连接池
* */
String parent=null;
/**
* JDBC连接对象
* */
private Connection conn=null;
/**
* 事务锁,执行事务时必须获取的锁对象
* */
Object transLocker=new Object();
/**
* 连接状态
* */
public int status;
/**
* 连接状态可以为下列三种状态之一
* */
public static final int FREE=1; //连接空闲
public static final int TAKING=2; //连接被占用,当前没有执行SQL语句
public static final int EXECUTING=3; //连接正在执行SQL语句
/**
* 记录集的类型
* */
//记录集只能前滚
public static final int TYPE_FORWARD_ONLY=ResultSet.TYPE_FORWARD_ONLY;
//记录集能前滚能后滚,记录集合忽略其他用户对记录集的修改
public static final int TYPE_SCROLL_INSENSITIVE=ResultSet.TYPE_SCROLL_INSENSITIVE;
//记录集能前滚能后滚,记录集合忽略其他用户对记录集的修改
public static final int TYPE_SCROLL_SENSITIVE=ResultSet.TYPE_SCROLL_SENSITIVE;
/**
* 是否正在执行sql语句
* */
private String sqlString=null;
/**
* 最近执行的SQL语句开始执行时间
* */
private Calendar startTime=null;
/**
* 最近执行的SQL语句结束执行时间,如果执行未完成,则endTime为空
**/
private Calendar endTime=null;
/**
* 修改时间2004/8/12,添加了和表级备份和恢复相关的接口
* */
Properties dbInfo=null;
public final static int WINDOWS =1;
public final static int UNIX=2;
public final static int LINUX=3;
public int backupRestoreTimout=10000;
private String dbUser=""; //登录数据库的用户名
private String dbPassword=""; //登录数据库的口令
// private String osUser=""; //执行rsh脚本的用户名
//private String osPassword=""; //登录mysql服务器的口令
private String backDir=""; //备份目录
private void initProperties(String url) throws JDBCException{
if (dbInfo == null) {
dbInfo = new Properties();
}
if ((url != null) && url.startsWith("jdbc:mysql://")) {
try {
dbInfo = parseURL(url, dbInfo);
} catch (SQLException e) {
throw new JDBCException(ExceptionHandle.getMessage(e));
}
}else{
throw new JDBCException(JDBCCnnMgrConstants.INVALID_URL);
}
}
/**
* 方法添加时间 2004/9/27
* 通过jdb执行mysql的backup命令
* @param tableName 要备份的表.
* @param backDir 存放备份文件的目录.
* @param dbName 要备份的数据库名称.
* @JDBCException 备份时出错或者参数不正确
* */
private void backup(String tableName,String backDir,String dbName)
throws JDBCException{
String flush="flush tables";
//System.out.println(flush);
executeUpdate(flush);
String sql="";
sql="backup table ";
sql=sql+tableName+" to ";
sql=sql+"'"+backDir+"/"+dbName+"'";
//System.out.println(sql);
executeUpdate(sql);
}
/**
* 方法添加时间 2004/8/10
* 备份名为tableName的表到目录backDir. 在备份之前必须指定程序所在的
* 操作系统平台,平台候选项有WINDOWS, LINUX, UNIX. 当程序和mysql服务器在同一
* 机器上面的时候,用该方法做备份。
* @param tableName 要备份的表.
* @param backDir 存放备份文件的目录.
* @param flatform mysql服务器的操作系统平台.
* @JDBCException 备份时出错或者参数不正确
* */
private void backupTable(String tableName,String backDir,int platform)
throws JDBCException{
//格式:./backup.sh 端口 mysql主目录 数据库 用户名 密码 表名
String cmd="";
String shellName="";
if(platform==WINDOWS){
//shellName="./mysqlsh/windows/backup.sh";
throw new JDBCException("WINDOWS_NOT_SUPPORT");
}else if(platform==LINUX){
shellName="/usr/local/mysql/backup-restore/backup_srv.sh";
}else if(platform==UNIX){
shellName="/usr/local/mysql/backup-restore/backup_srv.sh";
}else{
throw new JDBCException(JDBCCnnMgrConstants.PLATFORM_TYPE);
}
cmd=shellName+" ";
cmd=cmd+dbInfo.getProperty("PORT", "3306")+" ";
cmd=cmd+backDir+" ";
String dbName=dbInfo.getProperty("DBNAME");
cmd=cmd+dbName+" ";
cmd=cmd+dbUser+" ";
cmd=cmd+dbPassword+" ";
cmd=cmd+tableName+" ";
//System.out.println(cmd);
exec(cmd);
//void backup(String tableName,String backDir,String dbName);
backup(tableName,backDir,dbName);
}
/**
* 方法添加时间 2004/8/10
* 备份名为tableName的表到目录backDir,该方法具有平台自适应性,
* 无需指定程序所在的操作系统平台,目前不支持windows平台。
* @param tableName 要备份的表.
* @param backDir 存放备份文件的目录.
* @JDBCException 备份时出错或者参数不正确
* */
public void backupTableAuto(String tableName,String backDir)
throws JDBCException{
int platform;
String osName=System.getProperty("os.name");
if(osName.toUpperCase().indexOf("WINDOWS")>=0) platform=WINDOWS;
else if(osName.toUpperCase().indexOf("LINUX")>=0) platform=LINUX;
else platform=UNIX;
backupTable(tableName,backDir,platform);
}
/**
* 方法添加时间 2004/8/10
* 备份名为tableName的表到默认的备份目录,该方法具有平台自适应性,
* 无需指定程序所在的操作系统平台,目前不支持windows平台。
* @param tableName 要备份的表.
* @JDBCException 备份时出错或者参数不正确
* */
public void backupTableAuto(String tableName)
throws JDBCException{
//if(this.backDir.equals("")) this.setBackDir("/usr/local/mysql");
backupTableAuto(tableName,this.backDir);
}
/**
* 判断ip地址是否为本地ip地址
* @param host host为需要判断的ip地址
* @return true 表示host为本机器的ip地址
* false 表示host为远程的ip地址
* */
private static boolean isLocal(String host){
if(host.equals("127.0.0.1")) return true;
if(host.equals("localhost")) return true;
try {
InetAddress ia=InetAddress.getLocalHost();
String name=ia.getHostName();
InetAddress ias[]=InetAddress.getAllByName(name);
for(int i=0;i<ias.length;i++){
if((ias[i].getHostAddress()).equals(host)) return true;
}
} catch (UnknownHostException e) {
return false;
}
return false;
}
/**
* 方法添加时间 2004/9/27
* 通过jdb执行mysql的restore命令
* @param tableName 要恢复的表.
* @param backDir 存放恢复文件的目录.
* @param dbName 要恢复的数据库名称.
* @JDBCException 恢复时出错或者参数不正确
*/
private void restore(String tableName,String backDir,String dbName)
throws JDBCException
{
String flush="flush tables";
//System.out.println(flush);
executeUpdate(flush);
String sql="";
sql=sql+"restore table "+tableName;
sql=sql+" from ";
sql=sql+"'"+backDir+"/"+dbName+"'";
//System.out.println(sql);
executeUpdate(sql);
}
/**
* 方法添加时间 2004/8/10
* 从备份目录backDir恢复表tableName.在做表恢复之前要指定程序所在的操作系统平台,
* 后选项有WINDOWS, LINUX, UNIX. 当程序和mysql服务器在同一台机器上面
* 的时候是使用该方法
* @param tableName 要恢复的表名.
* @param backDir 存放备份文件的位置.
* @param flatform 程序所在的平台.
* @JDBCException 恢复表出错或者参数非法
* */
private void restoreTable(String tableName,String backDir,int platform)
throws JDBCException{
//格式:restore.sh 端口 mysql主目录 数据库 用户名 密码 表名
String cmd="";
String shellName="";
if(platform==WINDOWS){
// shellName="./mysqlsh/windows/restore.sh";
throw new JDBCException(JDBCCnnMgrConstants.WINDOWS_NOT_SUPPORT);
}else if(platform==LINUX){
shellName="/usr/local/mysql/backup-restore/restore_srv.sh";
}else if(platform==UNIX){
shellName="/usr/local/mysql/backup-restore/restore_srv.sh";
}else{
throw new JDBCException(JDBCCnnMgrConstants.PLATFORM_TYPE);
}
cmd=shellName+" ";
cmd=cmd+dbInfo.getProperty("PORT", "3306")+" ";
cmd=cmd+backDir+" ";
String dbName=dbInfo.getProperty("DBNAME");
cmd=cmd+dbName+" ";
cmd=cmd+dbUser+" ";
cmd=cmd+dbPassword+" ";
cmd=cmd+tableName+" ";
//System.out.println(cmd);
exec(cmd);
restore(tableName,backDir,dbName);
}
/**
* 方法添加时间 2004/8/10
* 从备份目录backDir恢复表tableName.该方法具有平台自适应性,
* 无需指定程序所在的操作系统平台,目前不支持windows验证。
* @param tableName 要恢复的表名.
* @param backDir 存放备份文件的位置.
* @param flatform 程序所在的平台.
* @JDBCException 恢复表出错或者参数非法
* */
public void restoreTableAuto(String tableName,String backDir)
throws JDBCException{
int platform;
String osName=System.getProperty("os.name");
if(osName.toUpperCase().indexOf("WINDOWS")>=0) platform=WINDOWS;
else if(osName.toUpperCase().indexOf("LINUX")>=0) platform=LINUX;
else platform=UNIX;
restoreTable(tableName,backDir,platform);
}
/**
* 方法添加时间 2004/8/10
* 从备份目录backDir恢复表tableName.该方法具有平台自适应性,
* 无需指定程序所在的操作系统平台,目前不支持windows验证。
* @param tableName 要恢复的表名.
* @JDBCException 恢复表出错或者参数非法
* */
public void restoreTableAuto(String tableName)
throws JDBCException{
restoreTableAuto(tableName,backDir);
}
/**
* 方法添加时间 2004/8/10
* 执行外部shell命令
* @param command shell命令
* @JDBCException 执行外部shell命令出错
* */
private void exec(String command) throws JDBCException{
Runtime rt = Runtime.getRuntime();
Process ps=null;
String result=null;
String err=null;
int exitValue=0;
try {
ps=rt.exec(command);
result = Executor.loadStream(ps.getInputStream());
err=Executor.loadStream(ps.getErrorStream());
exitValue=ps.waitFor();
} catch (Exception e) {
throw new JDBCException(ExceptionHandle.getMessage(e));
////.log(//.WARN,e.getMessage());
}
if(err!=null&&!err.equals(""))
throw new JDBCException(err);
if(result!=null&&!result.equals(""))
throw new JDBCException(result);
if(exitValue!=0)
throw new JDBCException("Operation failed, error code:"+exitValue);
}
/**
* 方法添加时间 2004/8/10
* 解析数据库连接字符串url
* @param url 数据库连接字符串.
* @param defaults 包含缺省值的属性列表
* @JDBCException url格式错误
* */
private static Properties parseURL(String url, Properties defaults)
throws SQLException {
Properties urlProps = new Properties(defaults);
if (url == null) {
return null;
} else {
/*
* Parse parameters after the ? in the URL and remove
* them from the original URL.
*/
int index = url.indexOf("?");
if (index != -1) {
String paramString = url.substring(index + 1, url.length());
url = url.substring(0, index);
StringTokenizer queryParams = new StringTokenizer(paramString,
"&");
while (queryParams.hasMoreTokens()) {
StringTokenizer vp = new StringTokenizer(queryParams
.nextToken(), "=");
String param = "";
if (vp.hasMoreTokens()) {
param = vp.nextToken();
}
String value = "";
if (vp.hasMoreTokens()) {
value = vp.nextToken();
}
if ((value.length() > 0) && (param.length() > 0)) {
urlProps.put(param, value);
}
}
}
StringTokenizer st = new StringTokenizer(url, ":/", true);
if (st.hasMoreTokens()) {
String protocol = st.nextToken();
if (protocol != null) {
if (!protocol.equalsIgnoreCase("jdbc")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the colon following 'jdbc'
if (st.hasMoreTokens()) {
String colon = st.nextToken();
if (colon != null) {
if (!colon.equals(":")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for sub-protocol to be mysql
if (st.hasMoreTokens()) {
String subProto = st.nextToken();
if (subProto != null) {
if (!subProto.equalsIgnoreCase("mysql")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the colon following 'mysql'
if (st.hasMoreTokens()) {
String colon = st.nextToken();
if (colon != null) {
if (!colon.equals(":")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the "//" of the URL
if (st.hasMoreTokens()) {
String slash = st.nextToken();
String slash2 = "";
if (st.hasMoreTokens()) {
slash2 = st.nextToken();
}
if ((slash != null) && (slash2 != null)) {
if (!slash.equals("/") && !slash2.equals("/")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -