All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.eforceconfig.jdbc.DBConnection Maven / Gradle / Ivy

The newest version!
package com.google.code.eforceconfig.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.oro.util.CacheLRU;

/**
 * Easy to use java.sql.Connection wrapper
 * 
 * Connection conn= 
 * 
 * DBConnection dbconn= new DBConnection(conn);
 * EntityConfig entity= config.getEntity("customer");
 * 
 * DBStatement dml= new DBStatement(entity.getSQLStatement("MY_CUSTOMER_INSERT_STATEMENT"));
 * dml.bindVariable("fname","Andrea");
 * dml.bindVariable("lname","Gariboldi");
 * 
 * dbconn.executeDML(dml);
 * 
 * dbconn.commit();
 */
public class DBConnection 
{
  private static Logger logger= Logger.getLogger(DBConnection.class);
  
  private Connection oc;
  private boolean debug= false;
  private int maxstmtbc=1000;
  private int execstmt=0;
  private static final int mdCacheSize= 100;
  private static CacheLRU mdCache;
  
  public DBConnection(Connection conn)
  {    
   try
   {
     oc=  conn;
     if (mdCache==null)
         mdCache= new CacheLRU(mdCacheSize);
     debug= logger.isInfoEnabled();
   }
   catch (Exception e)
   {
    logger.error("",e);
   }
  }

  public void setMaxStmtBeforeCommit(int no)
  {
    this.maxstmtbc= no;
  }

  public void setDebug(boolean debug)
  {
    this.debug= debug;
  }

  public void close()
  {
       try
       {
         if (debug) logger.info("DBConnection close()");
         oc.close();
       }
       catch (Exception e)
       {}
  }

  public Connection getConnection()
  {
    return oc;
  }
   
  public DBData getData(DBStatement query)
  throws Exception
  {
    return _getData(query, oc, debug);
  }
  
  public DBFetchableData getFetchableData(DBStatement query)
  throws Exception
  {
     return _getFetchableData(query,oc,debug);
  }

  public void executeDMLC(DBStatement dml)
  throws SQLException
  {
     
     executeDML(dml);

     if (++execstmt%maxstmtbc==0) 
     {
        commit();
        logger.warn("Commit point reached: "+execstmt);
     }
  }

  public void executeDML(DBStatement dml)
  throws SQLException
  {
         SQLException ex= null;
         if (debug) logger.info("DBConnection executeDML(): dml: "+dml.getStatement());
     
         PreparedStatement stmt= oc.prepareStatement(dml.getStatement());
         for (int i=0;i0)
              stmt.setFetchSize(query.getFetchSize());

          Object o; // aggiunto per driver ODBC
          for (int i=0;i0)
       stmt.setFetchSize(query.getFetchSize());

      for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy