Advertisement
Guest User

Untitled

a guest
Nov 12th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. public class DB {
  7.  
  8.     private static DB instance;
  9.     private static final int MAX_CON = 5;
  10.     private static final Connection[] bafer = new Connection[MAX_CON];
  11.     private int first = 0, last = 0, free = MAX_CON;
  12.  
  13.     private DB() { //za MySQL
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver");
  16.             for (int i = 0; i < MAX_CON; i++) {
  17.                 bafer[i] = DriverManager.getConnection("jdbc:mysql://localhost:3306/reklamacije", "root", "root");
  18.             }
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.     /* za Access
  24.      private DB(){
  25.      try{
  26.      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  27.      for(int i=0; i<MAX_CON; i++)
  28.      bafer[i] = DriverManager.getConnection("jdbc:odbc:dbkorisnik");
  29.      }catch(Exception e){}
  30.      }
  31.      */
  32.  
  33.     public static DB getInstance() {
  34.         if (instance == null) {
  35.             instance = new DB();
  36.         }
  37.         return instance;
  38.     }
  39.  
  40.     public synchronized Connection getConnection() {
  41.         if (free == 0) {
  42.             return null;
  43.         }
  44.         free--;
  45.         Connection con = bafer[first];
  46.         first = (first + 1) % MAX_CON;
  47.         return con;
  48.     }
  49.  
  50.     public synchronized void putConnection(Connection con) {
  51.         if (con == null) {
  52.             return;
  53.         }
  54.         free++;
  55.         bafer[last] = con;
  56.         last = (last + 1) % MAX_CON;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement