fairnet/src/main/java/seed/utils/SeedSqlCon.java

276 lines
7.0 KiB
Java

package seed.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
public class SeedSqlCon {
/*@Value("${Dspring.profiles.active}")
private String springProfilesActive;*/
private String springProfilesActive = System.getProperty("spring.profiles.active");
private Logger log = Logger.getLogger(this.getClass());
private Connection connection = null;
private String dbType = "";
private String dbVersion = "";
public SeedSqlCon(String jndiName){
SeedProperties seedProperties = new SeedProperties();
try{
seedProperties.configPropertiesInit();
setDbType(seedProperties.getConfigProperties().getProperty("database").toUpperCase());
setDbVersion(seedProperties.getConfigProperties().getProperty("database.version").toUpperCase());
/* 운영환경에따른 분기 */
String jndi = "jndi/";
jndi += springProfilesActive;
if ((jndiName != null) && (!("".equals(jndiName)))) {
jndi = jndiName;
}
if (seedProperties.getConfigProperties().getProperty("was").equals("tomcat")) {
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
if (envCtx != null) {
DataSource ds = (DataSource)envCtx.lookup(jndi);
if (ds != null){
this.connection = ds.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equalsIgnoreCase("jeus")){
InitialContext ctx = new InitialContext();
if(ctx != null){
DataSource ds = (DataSource)ctx.lookup(jndi);
if(ds != null){
this.connection = ds.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equals("weblogic")){
Context envCtx = new InitialContext();
if(envCtx != null){
DataSource ds = (DataSource)envCtx.lookup(jndi);
if(ds != null){
connection = ds.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equals("jboss")){
InitialContext ctxt = new InitialContext();
if (ctxt != null) {
DataSource ds = (DataSource) ctxt.lookup("java:jboss/"+jndi);
if (ds != null){
connection = ds.getConnection();
}
}
}
}catch (NamingException e){
log.error("CHECK ERROR:",e);
}catch(SQLException e){
log.error("CHECK ERROR:",e);
}
}
public SeedSqlCon(String jndiName,String dbType, String dbVersion){
SeedProperties seedProperties = new SeedProperties();
try{
seedProperties.configPropertiesInit();
setDbType(dbType);
setDbVersion(dbVersion);
/* 운영환경에따른 분기 */
String jndi = "jndi/";
jndi += springProfilesActive;
if ((jndiName != null) && (!("".equals(jndiName)))) {
jndi = jndiName;
}
if (seedProperties.getConfigProperties().getProperty("was").equalsIgnoreCase("tomcat")) {
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
if (envCtx != null) {
DataSource dataSource = (DataSource)envCtx.lookup(jndi);
if (dataSource != null){
this.connection = dataSource.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equalsIgnoreCase("jeus")){
InitialContext ctx = new InitialContext();
if(ctx != null){
DataSource ds = (DataSource)ctx.lookup(jndi);
if(ds != null){
this.connection = ds.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equalsIgnoreCase("weblogic")){
Context envCtx = new InitialContext();
if(envCtx != null){
DataSource ds = (DataSource)envCtx.lookup(jndi);
if(ds != null){
this.connection = ds.getConnection();
}
}
}else if(seedProperties.getConfigProperties().getProperty("was").equals("jboss")){
InitialContext ctxt = new InitialContext();
if (ctxt != null) {
DataSource ds = (DataSource) ctxt.lookup("java:jboss/"+jndi);
if (ds != null){
connection = ds.getConnection();
}
}
}
}catch (NamingException e){
log.error("CHECK ERROR:",e);
}catch(SQLException e){
log.error("CHECK ERROR:",e);
}
}
public SeedSqlCon(String dbType, String dbVersion, String dbIp, String dbPort, String dbName, String dbUser, String dbPw){
try{
setDbType(dbType);
setDbVersion(dbVersion);
if(dbType.equals("MSSQL")){
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
setConnection(DriverManager.getConnection("jdbc:sqlserver://"+dbIp+":"+dbPort+";DatabaseName="+dbName+"", ""+dbUser+"", ""+dbPw+""));
}else if(dbType.equals("MYSQL")){
Class.forName("com.mysql.jdbc.Driver");
setConnection(DriverManager.getConnection("jdbc:mysql://"+dbIp+":"+dbPort+"/"+dbName+"", ""+dbUser+"", ""+dbPw+""));
}else if(dbType.equals("ORACLE")){
Class.forName("oracle.jdbc.driver.OracleDriver");
if(dbName.indexOf("SERVICE:") > -1){
setConnection(DriverManager.getConnection("jdbc:oracle:thin:@"+dbIp+":"+dbPort+"/"+dbName.split(":")[1]+"", ""+dbUser+"", ""+dbPw+""));
}else{
setConnection(DriverManager.getConnection("jdbc:oracle:thin:@"+dbIp+":"+dbPort+":"+dbName+"", ""+dbUser+"", ""+dbPw+""));
}
}else if(dbType.equals("TIBERO")){
Class.forName("com.tmax.tibero.jdbc.TbDriver");
setConnection(DriverManager.getConnection("jdbc:tibero:thin:@"+dbIp+":"+dbPort+":"+dbName+"", ""+dbUser+"", ""+dbPw+""));
}
}catch (ClassNotFoundException e){
log.error("CHECK ERROR:",e);
}catch(SQLException e){
log.error("CHECK ERROR:",e);
}
}
public void setSeedSqlDispose() {
if (connection != null){
try{
connection.close();
connection = null;
}catch(SQLException e){
log.error("CHECK ERROR:",e);
}
}
}
public PreparedStatement seedPrepareStatement(String strSql) throws SQLException {
return connection.prepareStatement(strSql);
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
connection.setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
return connection.getAutoCommit();
}
public void commit() throws SQLException {
connection.commit();
}
public void rollback() throws SQLException {
connection.rollback();
}
public String getDbType() {
return SeedUtils.setReplaceNull(dbType);
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public String getDbVersion() {
return dbVersion;
}
public void setDbVersion(String dbVersion) {
this.dbVersion = SeedUtils.setReplaceNull(dbVersion);
}
}