Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package org.askalon.provenance.gpdatabase.impl;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import org.apache.log4j.Logger;
  12.  
  13.  
  14. public class DatabaseManager
  15. {
  16.    
  17.     private ResultSet rs1, rs2, rs3, rs4;
  18.     private Statement st1, st2, st3, st4;
  19.     private static final Logger logger = Logger.getLogger(DatabaseManager.class);
  20.     private static Connection connection = null;
  21.    
  22.     // ======= SINGLETON DEFS START HERE===========//
  23.     private static DatabaseManager instance = null;
  24.  
  25.     private static final Logger LOGGER = Logger.getLogger(DatabaseManager.class);
  26.  
  27.     public DatabaseManager()
  28.     {
  29.     }
  30.  
  31.     public static DatabaseManager getInstance()
  32.     {
  33.         if (instance == null)
  34.         {
  35.  
  36.             create();
  37.         }
  38.  
  39.         return instance;
  40.     }
  41.  
  42.     private static DatabaseManager create()
  43.     {
  44.         instance = new DatabaseManager();
  45.         return instance;
  46.     }
  47.     // ==============SINGLETON DEFS END HERE===============//
  48.  
  49.     public static Connection getConnection()
  50.     {
  51.         if (connection == null)
  52.             connection = createConnection();
  53.         return connection;
  54.     }
  55.  
  56.     private static Connection createConnection()
  57.     {
  58.         try
  59.         {
  60.             logger.debug("Now trying to Connect to DB");
  61.             connection = DriverManager.getConnection("jdbc:postgresql://" + (InetAddress.getLocalHost().getHostName().equals("junaid-desktop") ? "127.0.0.1" : "192.168.64.183") + "/" + "GPP",
  62.                     "USER", "PASS");
  63.  
  64.         }
  65.         catch (SQLException se)
  66.         {
  67.             logger.error("Connectivity Failed");
  68.             logger.error(se.getMessage());
  69.         }
  70.         catch (UnknownHostException e)
  71.         {
  72.             e.printStackTrace();
  73.         }
  74.         return connection;
  75.     }
  76.  
  77.     public void closeConnection()
  78.     {
  79.         if (connection != null)
  80.         {
  81.             try
  82.             {
  83.                 DatabaseManager.connection.close();
  84.                 LOGGER.debug("Connection Closed");
  85.             }
  86.             catch (SQLException e)
  87.             {
  88.                 LOGGER.error("Can not close the database connection");
  89.             }
  90.         }
  91.     }
  92.  
  93.     public Statement createStatement()
  94.     {
  95.         Statement stmt = null;
  96.         try
  97.         {
  98.             stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  99.         }
  100.         catch (SQLException se)
  101.         {
  102.             LOGGER.error("Couldn't create statement.");
  103.             LOGGER.error(se.getStackTrace());
  104.         }
  105.         return (stmt);
  106.     }
  107.  
  108.  
  109.     public boolean PerformDatabaseAction(String ActionCommand)
  110.     {
  111.         System.out.println("Storage ITEM is "+ ActionCommand);
  112.         return (ActionCommand.length()>10?true:false);
  113.     }
  114. }
  115.  
  116.  
  117. //public boolean PerformDatabaseAction(String ActionCommand)
  118. //{
  119. //  String SQL=makeStatement(ActionCommand);
  120. // 
  121. //  st1 = createStatement();
  122. //  try
  123. //  {
  124. //      rs1=st1.executeQuery(SQL);
  125. //     
  126. //  }
  127. //  catch (SQLException e)
  128. //  {
  129. //      e.printStackTrace();
  130. //  }
  131. //  return false;
  132. //}
  133. //
  134. //private String makeStatement(String actionCommand)
  135. //{
  136. //  return null;
  137. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement