📄 connutils.java
字号:
/*
* Created on 2004-3-23
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.zosatapo.xls.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author zosatapo
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ConnUtils
{
public static Connection getConnection(StoreConfig storeConfig)
throws SQLException
{
int cMethod=getConnectionMethod(storeConfig);
String cString=storeConfig.getURL();
String cUserName=storeConfig.getUserName();
String cPassword=storeConfig.getPassword();
String cDriver=storeConfig.getDriverClass();
if(cDriver!=null)
{
try
{
Class.forName(cDriver);
}
catch(Exception ex0)
{
throw new SQLException("["+ConnUtils.class.getName()+"] Can not load the database jdbc driver."+cDriver+"");
}
}
switch(cMethod)
{
case ConnectionFactory.CONN_DATESOURCE :
return ConnectionFactory.getConnectionByJDNI(cString);
case ConnectionFactory.CONN_DATESOURCE_LOGIN :
return ConnectionFactory.getConnectionByJDNI(cString,cUserName,cPassword);
case ConnectionFactory.CONN_DRIVERMANAGER :
return ConnectionFactory.getConnection(cString);
case ConnectionFactory.CONN_DRIVERMANAGER_LOGIN :
return ConnectionFactory.getConnection(cString,cUserName,cPassword);
default:
return ConnectionFactory.getConnectionByJDNI(ConnectionFactory.DEFAULT_DATASOURCE_JNDI);
}
}
//--------------------------------------------------------------------------
// 释放数据库连接 zosatapo 2003-10-17 13:11
//--------------------------------------------------------------------------
public static void cleanupNoThrow(Statement stmt)
{
if(stmt!=null)
{
try{stmt.close();}catch(SQLException ex){}
}
}
public static void cleanupNoThrow(PreparedStatement pstmt)
{
if(pstmt!=null)
{
try{pstmt.close();}catch(SQLException ex){}
}
}
public static void cleanupNoThrow(Connection conn)
{
if(conn!=null)
{
try{conn.close();}catch(SQLException ex){}
}
}
public static void cleanupNoThrow(Connection conn,Statement stmt)
{
if(stmt!=null)
{
try{stmt.close();}catch(SQLException ex){}
}
if(conn!=null)
{
try{conn.close();}catch(SQLException ex){}
}
}
public static void cleanupNoThrow(Connection conn,PreparedStatement pstmt)
{
if(pstmt!=null)
{
try{pstmt.close();}catch(SQLException ex){}
}
if(conn!=null)
{
try{conn.close();}catch(SQLException ex){}
}
}
public static void cleanupThrow(Statement stmt)
throws SQLException
{
if(stmt!=null){stmt.close();}
}
public static void cleanupThrow(PreparedStatement pstmt)
throws SQLException
{
if(pstmt!=null){pstmt.close();}
}
public static void cleanupThrow(Connection conn)
throws SQLException
{
if(conn!=null){conn.close(); }
}
public static void cleanupThrow(Connection conn,Statement stmt)
throws SQLException
{
if(stmt!=null){stmt.close();}
if(conn!=null){conn.close(); }
}
public static void cleanupThrow(Connection conn,PreparedStatement pstmt)
throws SQLException
{
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close(); }
}
private static int getConnectionMethod(StoreConfig storeConfig)
{
if(storeConfig==null)
{
return ConnectionFactory.CONN_DEFAULT;
}
String connType=storeConfig.getType();
String connString=storeConfig.getURL();
String connUserName=storeConfig.getUserName();
String connPassword=storeConfig.getPassword();
int result=ConnectionFactory.CONN_DEFAULT;
String dataSource=ConnectionFactory.TYPE_DATA_SOURCE;
String ddriverManager=ConnectionFactory.TYPE_DRIVER_MANAGER;
if(dataSource.equalsIgnoreCase(connType))
{
if(connString==null)
{
result=ConnectionFactory.CONN_DEFAULT;
}
else
{
if(connUserName==null || connPassword==null)
{
result=ConnectionFactory.CONN_DATESOURCE;
}
else
{
result=ConnectionFactory.CONN_DATESOURCE_LOGIN;
}//end if(connUserName==null || connPassword==null)
}//~end if(connString==null)
}
else if(ddriverManager.equalsIgnoreCase(connType))
{
if(connUserName==null || connPassword==null)
{
result=ConnectionFactory.CONN_DRIVERMANAGER;
}
else
{
result=ConnectionFactory.CONN_DRIVERMANAGER_LOGIN;
}//end if(connUserName==null || connPassword==null)
}
else
{
result=ConnectionFactory.CONN_DEFAULT;
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -