Search code examples
jdbcapache-camelesbjbossfuseconnector

JBoss JDBC Dynamic Connector using restlet


I need to create dynamic JDBC Connector, i.e., it has to dynamically pick driver for different SQL.

The dataSource as bean in camelContext.xml works fine.

I am trying to create REST way so the credentials and other info to connect to DB will be taken as JSON and the operation must be carried,

Kindly help.


Solution

  • In the processor class, getDataSource method is called with few mandatory parameters : host, port, username, rdbmsType and password in model object.

    DataSource ds = DBCPDataSourceFactory.getDataSource(model);
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    connection = ds.getConnection();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(model.getQuery());
    

    Datasource class :

    public class DBCPDataSourceFactory {
    public static DataSource getDataSource(DBModel model) {
        String url = null;
        BasicDataSource ds = new BasicDataSource();
        if (model.getRdbms().toLowerCase().equals(DBConstants.POSTGRESQL)) {
            ds.setDriverClassName(DBConstants.POSTGRESQL_DRIVER);
            url = "jdbc:postgresql://" + model.getHost() + "/" + model.getDatabase();
            ds.setUrl(url); 
        } else if (model.getRdbms().toLowerCase().equals(DBConstants.ORACLE)) {
            ds.setDriverClassName(DBConstants.ORACLE_DRIVER);
            url = "jdbc:oracle:thin:@"+model.getHost()+":"+model.getPort()+":"+model.getDatabase();
            ds.setUrl(url);
        } else if (model.getRdbms().toLowerCase().equals(DBConstants.SQL_SERVER)) {
            ds.setDriverClassName(DBConstants.SQL_SERVER_DRIVER);           
            url = "jdbc:sqlserver://"+model.getHost()+":"+model.getPort()+";databaseName="+model.getDatabase();
            ds.setUrl(url);
        }
        ds.setUsername(model.getUsername());
        ds.setPassword(model.getPassword());
        return ds;
    }}