daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package io.micbunk.dommeln;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DatabaseManager
  6. {
  7.     private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  8.     private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/tmdb";
  9.     private static final String USERNAME = "root";
  10.     private static final String PASSWORD = "";
  11.    
  12.     private static Connection conn;
  13.     private static PreparedStatement stmt;
  14.     private static ResultSet result;
  15.    
  16.     private DatabaseManager()
  17.     {
  18.     conn = null;
  19.     stmt = null;
  20.     result = null;
  21.     }
  22.    
  23.     private static class DatabaseManagerInstanceHolder
  24.     {
  25.     private final static DatabaseManager instance = new DatabaseManager();
  26.     }
  27.    
  28.     public static DatabaseManager getInstance()
  29.     {
  30.     return DatabaseManagerInstanceHolder.instance;
  31.     }
  32.    
  33.     public static Connection getConnection()
  34.     {
  35.     return DatabaseManager.conn;
  36.     }
  37.    
  38.     public static PreparedStatement getStatement()
  39.     {
  40.     return DatabaseManager.stmt;
  41.     }
  42.    
  43.     public static void setStatement(PreparedStatement prepstmt)
  44.     {
  45.     DatabaseManager.stmt = prepstmt;
  46.     }
  47.    
  48.     public static ResultSet getResult()
  49.     {
  50.     return DatabaseManager.result;
  51.     }
  52.    
  53.     public static void setResult(ResultSet result)
  54.     {
  55.     DatabaseManager.result = result;
  56.     }
  57.    
  58.     public static Connection connect()
  59.     {
  60.     if (conn == null)
  61.     {
  62.         System.out.println("Connecting...");
  63.         try
  64.         {
  65.         Class.forName(DATABASE_DRIVER);
  66.             conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
  67.            
  68.             //stmt = conn.prepareStatement("SELECT * FROM user_info");
  69.             //result = stmt.executeQuery();
  70.            
  71.             System.out.println("Connection succeed!");
  72.         }
  73.         catch (ClassNotFoundException | SQLException e)
  74.         {
  75.         System.out.println("Connection failed!");
  76.         e.printStackTrace();
  77.         }
  78.     }
  79.    
  80.     return conn;
  81.     }
  82.    
  83.     public static void disconnect()
  84.     {
  85.     if(conn != null)
  86.     {
  87.         System.out.println("Disconnecting...");
  88.         try
  89.         {
  90.        
  91.         if(conn != null)
  92.         {
  93.             conn.close();
  94.             conn = null;
  95.         }
  96.  
  97.         if(stmt != null)
  98.         {
  99.             stmt.close();
  100.             stmt = null;
  101.         }
  102.            
  103.        
  104.         System.out.println("Disconnecion succeed!");
  105.         }
  106.         catch (SQLException e)
  107.         {
  108.         System.out.println("Disconnection failed!");
  109.         e.printStackTrace();
  110.         }
  111.     }
  112.     }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top