Facebook
From Morose Finch, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 224
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import org.apache.commons.dbcp.BasicDataSource;
  9. import org.flywaydb.core.Flyway;
  10.  
  11. public class DBConnection {
  12.        
  13.         private static final String DRIVER = "org.sqlite.JDBC";
  14.         private static final String JDBC_PATH = "jdbc:sqlite:resources/db/";
  15.         private static final String DB_NAME = "racingGameDB.db";
  16.         private static final String USERNAME="";
  17.         private static final String PASSWORD="";
  18.        
  19.         private static final DBConnection INSTANCE = new DBConnection();
  20.        
  21.         private BasicDataSource dataSource;
  22.        
  23.         private DBConnection(){
  24.                
  25.         }
  26.        
  27.         public static DBConnection getInstance(){
  28.                 return DBConnection.INSTANCE;
  29.         }
  30.        
  31.         public void init(){
  32.                 dataSource = new BasicDataSource();
  33.                 dataSource.setDriverClassName(DRIVER);
  34.                 dataSource.setUrl(JDBC_PATH + DB_NAME);
  35.                 dataSource.setUsername(USERNAME);
  36.                 dataSource.setUsername(PASSWORD);
  37.             final Flyway flyway = new Flyway();
  38.             flyway.setDataSource(dataSource);
  39.             flyway.clean();
  40.             flyway.migrate();
  41.         }
  42.        
  43.         public static void close(Connection connection, PreparedStatement preparedStatement) {
  44.                 try {
  45.                         if (connection != null) {
  46.                                 connection.close();
  47.                         }
  48.  
  49.                         if (preparedStatement != null) {
  50.                                 preparedStatement.close();
  51.                         }
  52.                 } catch (Exception e) {
  53.                         e.printStackTrace();
  54.                 }
  55.         }
  56.        
  57.         public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
  58.                 try {
  59.                         if (connection != null) {
  60.                                 connection.close();
  61.                         }
  62.  
  63.                         if (preparedStatement != null) {
  64.                                 preparedStatement.close();
  65.                         }
  66.  
  67.                         if (resultSet != null) {
  68.                                 resultSet.close();
  69.                         }                      
  70.                 } catch (Exception e) {
  71.                         e.printStackTrace();
  72.                 }
  73.         }
  74.  
  75.          public static Connection getConnection() throws SQLException {
  76.                     return DBConnection.getInstance().getDataSource().getConnection();
  77.         }
  78.          
  79.           public void close() {
  80.                     if (dataSource != null) {
  81.                       try {
  82.                         dataSource.close();
  83.                       } catch (final SQLException e) {
  84.                           e.printStackTrace();
  85.                       }
  86.                     }
  87.                   }
  88.          
  89.         //Thread will be close automatically data source
  90.         public void registerShutdownHook() {
  91.                     Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  92.                       @Override
  93.                       public void run() {
  94.                         close();
  95.                       }
  96.                     }));
  97.                   }
  98.        
  99.         public BasicDataSource getDataSource() {
  100.                 return dataSource;
  101.         }
  102.  
  103.         public void setDataSource(BasicDataSource dataSource) {
  104.                 this.dataSource = dataSource;
  105.         }
  106.        
  107. }
  108.