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.
DB_Enterprise Main configuration class for enterprise database connections.
Main Method
Signature
public static void main ( String [] args) throws IOException
Command-line arguments (not used in this implementation)
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:
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"
Enables clean shutdown of the database
Disables lock file creation for concurrent access
H2 URL
"jdbc:h2:file:./db_empresa_h2;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1"
Enables automatic server mode for concurrent connections
Keeps the database open until the JVM exits (-1 means never auto-close)
Connection Details
Default database username
Empty password (no authentication)
Implementation Flow
Configuration File Creation
Loading Configuration
Establishing Connection
// 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 ();
}