Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package books;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15.  *
  16.  * @author qasko
  17.  */
  18. public class Database {
  19.  
  20.     private static Connection connection;
  21.     private static final String URL = "jdbc:mysql://localhost/booksapp";
  22.     private static final String USERNAME = "root";
  23.     private static final String PASSWORD = "";
  24.  
  25.     public static Connection connect() throws SQLException {
  26.         try {
  27.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  28.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
  29.             Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
  30.         }
  31.  
  32.         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  33.         return connection;
  34.     }
  35.  
  36.     public static Connection getConnection() throws SQLException, ClassNotFoundException {
  37.         if (connection != null && !connection.isClosed()) {
  38.             return connection;
  39.         }
  40.         connect();
  41.         return connection;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement