Package org.atomojo.app.db

Source Code of org.atomojo.app.db.DBConnection

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.atomojo.app.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.TreeMap;

/**
*
* @author alex
*/
public class DBConnection {

   Connection connection;
   Map<Integer,PreparedStatement> statements;
  
   DBConnection(Connection connection)
   {
      this.connection = connection;
      this.statements = new TreeMap<Integer,PreparedStatement>();
   }
  
   public PreparedStatement getStatement(int id)
      throws SQLException
   {
      PreparedStatement statement = statements.get(id);
      statement.clearParameters();
      return statement;
   }
  
   public void addStatement(int id,String sql)
      throws SQLException
   {
      PreparedStatement prepared = connection.prepareStatement(sql);
      statements.put(id, prepared);
   }
  
   public void removeStatement(int id)
      throws SQLException
   {
      PreparedStatement statement = statements.remove(id);
      statement.close();
   }
  
   public Connection getConnection() {
      return connection;
   }
  
   public void close()
      throws SQLException
   {
      for (PreparedStatement statement : statements.values()) {
         statement.close();
      }
      statements.clear();
   }
  
}
TOP

Related Classes of org.atomojo.app.db.DBConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.