Java tutorial
package com.its.web.orm; import java.sql.SQLException; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.its.web.db.mappings.License; import com.its.web.db.mappings.LicenseType; import com.its.web.db.mappings.Product; import com.its.web.db.mappings.WebUser; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; @Service public class Database { @Value("${DATABASE_URL}") private String databaseUrl; @Value("${LOGIN}") private String login; @Value("${PASSWORD}") private String password; // private final static String DATABASE_URL = "jdbc:postgresql://192.168.56.56:5432/licenses"; // private final static String LOGIN = "postgres"; // private final static String PASSWORD = "admin"; // private final static String DATABASE_URL = "jdbc:postgresql://localhost:5432/licenses"; // private final static String LOGIN = "postgres"; // private final static String PASSWORD = "admin"; private Dao<Product, Long> productDao; private Dao<License, Long> licenseDao; private Dao<WebUser, Long> webUserDao; private Dao<LicenseType, Long> licenseTypeDao; public ConnectionSource connectionSource = null; @PostConstruct public void init() { try { connectionSource = new JdbcConnectionSource(databaseUrl, login, password); //Products productDao = DaoManager.createDao(connectionSource, Product.class); if (!productDao.isTableExists()) { TableUtils.createTable(connectionSource, Product.class); } //Licenses licenseDao = DaoManager.createDao(connectionSource, License.class); if (!licenseDao.isTableExists()) { TableUtils.createTable(connectionSource, License.class); } //WebUser webUserDao = DaoManager.createDao(connectionSource, WebUser.class); if (!webUserDao.isTableExists()) { TableUtils.createTable(connectionSource, WebUser.class); } //licenseTypeDao licenseTypeDao = DaoManager.createDao(connectionSource, LicenseType.class); if (!licenseTypeDao.isTableExists()) { TableUtils.createTable(connectionSource, LicenseType.class); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ConnectionSource getConnectionSource() { return connectionSource; } public void setConnectionSource(ConnectionSource connectionSource) { this.connectionSource = connectionSource; } public Dao<Product, Long> getProductDao() { return productDao; } public void setProductDao(Dao<Product, Long> productDao) { this.productDao = productDao; } public Dao<License, Long> getLicenseDao() { return licenseDao; } public void setLicenseDao(Dao<License, Long> licenseDao) { this.licenseDao = licenseDao; } public Dao<WebUser, Long> getWebUserDao() { return webUserDao; } public void setWebUserDao(Dao<WebUser, Long> webUserDao) { this.webUserDao = webUserDao; } public Dao<LicenseType, Long> getLicenseTypeDao() { return licenseTypeDao; } public void setLicenseTypeDao(Dao<LicenseType, Long> licenseTypeDao) { this.licenseTypeDao = licenseTypeDao; } public String getDatabaseUrl() { return databaseUrl; } public void setDatabaseUrl(String databaseUrl) { this.databaseUrl = databaseUrl; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }