Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package books;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author qasko
- */
- public class Database {
- private static Connection connection;
- private static final String URL = "jdbc:mysql://localhost/booksapp";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "";
- public static Connection connect() throws SQLException {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
- Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
- }
- connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
- return connection;
- }
- public static Connection getConnection() throws SQLException, ClassNotFoundException {
- if (connection != null && !connection.isClosed()) {
- return connection;
- }
- connect();
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement