Skip to main content

Overview

The DB_Enterprise class provides basic database connection configuration using an external config.ini file. It supports both HSQLDB and H2 database management systems and demonstrates how to load database configurations from a properties file.

Class Information

Class
public class
DB_EnterpriseMain configuration class for enterprise database connections.

Main Method

Signature

public static void main(String[] args) throws IOException
args
String[]
Command-line arguments (not used in this implementation)
throws
IOException
Thrown when there are issues reading or writing the configuration file

Configuration Constants

The class uses a properties file (config.ini) to manage database configuration:
driverHSQLDB
String
default:"hsqldb"
HSQLDB driver identifier
driverH2
String
default:"h2"
H2 driver identifier
nameDB
String
default:"SA"
Database username (default: SA)

Database URLs

Depending on the driver selected in the configuration, different JDBC URLs are constructed:

HSQLDB URL

"jdbc:hsqldb:file:/home/alumno/Escritorio/BBDD/db_empresa;shutdown=true;hsqldb.lock_file=false"
shutdown
boolean
default:"true"
Enables clean shutdown of the database
hsqldb.lock_file
boolean
default:"false"
Disables lock file creation for concurrent access

H2 URL

"jdbc:h2:file:./db_empresa_h2;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1"
AUTO_SERVER
boolean
default:"TRUE"
Enables automatic server mode for concurrent connections
DB_CLOSE_DELAY
int
default:"-1"
Keeps the database open until the JVM exits (-1 means never auto-close)

Connection Details

username
String
default:"SA"
Default database username
password
String
default:""
Empty password (no authentication)

Implementation Flow

// If config.ini doesn't exist, create it with defaults
properties.setProperty("driverH2", "h2");
properties.setProperty("driverHSQLDB", "hsqldb");
properties.setProperty("nameDB", "SA");
properties.store(new FileOutputStream("config.ini"), 
                 "Configuracion de la base de datos");

Usage Example

public class DB_Enterprise {
    public static void main(String[] args) throws IOException {
        // Variables for configuration
        String driverHSQLDB = "";
        String driverH2 = "";
        String nameDB = "";
        String url = "";
        
        File f = new File("config.ini");
        Properties properties = new Properties();
        
        // Check if config file exists
        if (!f.exists()) {
            // Create default configuration
            properties.setProperty("driverH2", "h2");
            properties.setProperty("driverHSQLDB", "hsqldb");
            properties.setProperty("nameDB", "SA");
            properties.store(new FileOutputStream("config.ini"), 
                           "Configuracion de la base de datos");
        } else {
            // Load existing configuration
            properties.load(new FileInputStream(f));
            driverHSQLDB = properties.getProperty("driverHSQLDB");
            driverH2 = properties.getProperty("driverH2");
            nameDB = properties.getProperty("nameDB");
        }
        
        // Determine URL based on driver
        if (driverHSQLDB.equals("hsqldb")) {
            url = "jdbc:hsqldb:file:/home/alumno/Escritorio/BBDD/db_empresa;shutdown=true;hsqldb.lock_file=false";
        } else if (driverH2.equals("h2")) {
            url = "jdbc:h2:file:./db_empresa_h2;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1";
        }
        
        try {
            Connection conn = DriverManager.getConnection(url, "SA", "");
            System.out.println("La conexion se realizo correctamente " + url);
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
}

Error Handling

The class catches SQLException during connection attempts and prints the stack trace for debugging.
catch(SQLException e) {
    e.printStackTrace();
}

Build docs developers (and LLMs) love