public static boolean changeDatabase(String DBname) { File dir = new File(PropertyManager.getInstance().getPathProperty("path_database")); Database rollbackDB = selectedDB; try { selectedDB.setDefaultDB(false); selectedDB.saveToFile(new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.shutdown(); HibernateUtil.selectDatabase(DBname); selectedDB.setDefaultDB(true); selectedDB.saveToFile(new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.initSessionFactory(); AlbumPane.getInstance().refresh(); BottomBarPane.getInstance().refreshStats(); } catch (Exception ex) { MessagePaneManager.showExceptionPane(ex, true); try { selectedDB.setDefaultDB(false); selectedDB.saveToFile( new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.shutdown(); HibernateUtil.selectDatabase(rollbackDB.getName()); selectedDB.setDefaultDB(true); selectedDB.saveToFile( new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); } catch (Exception e) { logger.error("Error during the rollback", e); } return false; } return true; }
public static void initSessionFactory() throws FileNotFoundException, IOException, HibernateException, SQLException { types.put(POSTGRESQL, LocaleManager.getInstance().getString("db_postgresql")); types.put(MYSQL, LocaleManager.getInstance().getString("db_mysql")); types.put(HSQLDB, LocaleManager.getInstance().getString("db_hsqldb")); String dbName = HibernateUtil.getDefaultDatabase(); try { HibernateUtil.selectDatabase(dbName); } catch (FileNotFoundException e) { Database newDatabase = new Database(); newDatabase.setName("database"); newDatabase.setDefaultDB(true); newDatabase.setDialect(HSQLDB); newDatabase.setDriver(getDriverClass(HSQLDB)); newDatabase.setUsername("sa"); newDatabase.setPassword(""); newDatabase.setUrl( "jdbc:hsqldb:file:" + PropertyManager.getInstance().getPathProperty("path_resources") + PropertyManager.sep + "database"); newDatabase.saveToFile( new File( PropertyManager.getInstance().getPathProperty("path_database") + PropertyManager.sep + "database.properties")); File coverDir = new File( PropertyManager.getInstance().getPathProperty("path_cover") + PropertyManager.sep + "database"); if (!coverDir.mkdir()) { MessagePaneManager.showCheckErrorPane( LocaleManager.getInstance().getString("error_cover_dir")); } HibernateUtil.selectDatabase("database"); } HibernateUtil.loadSessionFactory(selectedDB); DatabaseUpgrader du = DatabaseUpgrader.getInstance(); try { if (!du.checkDatabase(selectedDB.getDriver(), selectedDB.getHibernateDialect())) du.upgradeDatabase(); } catch (SQLException e) { MessagePaneManager.showExceptionPane(e, true); e.printStackTrace(); } }
public static void createDatabase(DatabaseGuiObject dgo) throws FileNotFoundException, IOException { Database db = new Database(); db.setName(dgo.getName()); db.setDefaultDB(dgo.isDefaultDB()); db.setDialect(dgo.getDialect()); if (dgo.getDialect().equals(HSQLDB)) { db.setUrl( "jdbc:hsqldb:file:" + PropertyManager.getInstance().getPathProperty("path_resources") + PropertyManager.sep + dgo.getName()); db.setPassword(""); db.setUsername("sa"); } else { db.setUrl(dgo.getUrl()); db.setPassword(dgo.getPassword()); db.setUsername(dgo.getUsername()); } db.setDriver(HibernateUtil.getDriverClass(dgo.getDialect())); db.saveToFile( new File( PropertyManager.getInstance().getPathProperty("path_database") + PropertyManager.sep + dgo.getName() + ".properties")); if (db.isDefaultDB()) { HibernateUtil.setDatabaseToDefault(db, false); } File coverDir = new File( PropertyManager.getInstance().getPathProperty("path_cover") + PropertyManager.sep + dgo.getName()); if (!coverDir.mkdir()) { MessagePaneManager.showCheckErrorPane( LocaleManager.getInstance().getString("error_cover_dir")); } }