Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 2.49 KB

README.md

File metadata and controls

60 lines (49 loc) · 2.49 KB

Database

Simple Database wrapper written in Java to easily execute storedProcedure with flexible functions to meet requirements for all cases.

In case one wants to enable logging then corresponding slf4j implementation for respective logger will have to be added as a dependency.

Time taken for establishing DB connection as well as for executing the query is logged at INFO level.

How to use it !!

Create DatabaseConfig

  • For Single Connection
DatabaseConfig MSSQL_DATABASE_CONFIG = new DatabaseConfig("TEST_MASTER", 
                                                        DatabaseTypeImpl.MSSQL, 
                                                        "test_user", 
                                                        "test_password", 
                                                        "net.sourceforge.jtds.jdbc.Driver", 
                                                        "jdbc:jtds:sqlserver://localhost;databaseName=TEST_MASTER");
  • For Pooled Connections *Create PoolProperties and Pass it to DatabaseConfig
PoolProperties POOL_PROPERTIES = new PoolProperties() {{
        setTestOnBorrow(true);
        setTestOnReturn(true);
    }};
DatabaseConfig MSSQL_POOLED_DATABASE_CONFIG = new DatabaseConfig("TEST_MASTER", 
                                                        DatabaseTypeImpl.MSSQL, 
                                                        "test_user", 
                                                        "test_password", 
                                                        "net.sourceforge.jtds.jdbc.Driver", 
                                                        "jdbc:jtds:sqlserver://localhost;databaseName=TEST_MASTER", 
                                                        POOL_PROPERTIES);

Extend abstract class Database and match the constructor

public class DatabaseTest extends Database {
    public DatabaseTest(DatabaseConfig databaseConfig) {
            super(databaseConfig);
    }
}

Initialize the Derived Class by passing DatabaseConfig to it

DatabaseTest mssqlDB = new DatabaseTest(MSSQL_DATABASE_CONFIG);
DatabaseTest mssqlPooledDB = new DatabaseTest(MSSQL_POOLED_DATABASE_CONFIG);

Use the Derived Class object to executeQuery

StoredProcedureCall<String> storedProcedureCall = new StoredProcedureCall<>("sp_server_info", resultSet -> (resultSet.getString("attribute_name")));
List<String> values = database.executeQuery(storedProcedureCall);
System.out.println(values);