List of usage examples for org.springframework.jdbc.datasource DriverManagerDataSource getConnection
@Override public Connection getConnection() throws SQLException
From source file:web.mvc.servlets.MyServlet.java
private void fillDataFromDataSource(List<InfoItem> data, DriverManagerDataSource dataSource) throws SQLException { try (Connection connect = dataSource.getConnection()) { DatabaseMetaData metaData = connect.getMetaData(); data.add(new InfoItem("database name", metaData.getDatabaseProductName())); data.add(new InfoItem("database version", metaData.getDatabaseProductVersion())); data.add(new InfoItem("driver name", metaData.getDriverName())); data.add(new InfoItem("driver version", metaData.getDriverVersion())); }/*from w ww .j a va 2s. c o m*/ }
From source file:controladores.DepartamentosController.java
@Override public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { ModelAndView mv = new ModelAndView("departamentos"); DriverManagerDataSource dataSource; dataSource = (DriverManagerDataSource) this.getBean("dataSource", hsr.getServletContext()); this.cn = dataSource.getConnection(); mv.addObject("listadepartamentos", this.getDepartamentos()); return mv;/*from w w w. j av a 2s . c o m*/ }
From source file:controladores.EnfermoController.java
private Connection getConnection(ServletContext servlet) { if (this.cn == null) { DriverManagerDataSource dataSource; dataSource = (DriverManagerDataSource) this.getBean("xeBBDD", servlet); try {// ww w. ja va2 s .c o m this.cn = dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } } return this.cn; }
From source file:controladores.PeliculasController.java
private Connection getConnection(ServletContext servlet) { if (this.conexion == null) { DriverManagerDataSource dataSource; dataSource = (DriverManagerDataSource) this.getBean("dataSource", servlet); try {//from w w w .ja va 2s. c o m this.conexion = dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } } return this.conexion; }
From source file:com.iucosoft.eavertizare.dao.impl.ClientsDaoImpl.java
@Override public List<Client> findAllClientsForFirmaRemote(Firma firma) { DriverManagerDataSource dataSourceClient = new DriverManagerDataSource(); dataSourceClient.setDriverClassName(firma.getConfiguratii().getDriver()); dataSourceClient.setUrl(firma.getConfiguratii().getUrlDb()); dataSourceClient.setUsername(firma.getConfiguratii().getUsername()); dataSourceClient.setPassword(firma.getConfiguratii().getPassword()); String query = "select * from " + firma.getConfiguratii().getTabelaClienti(); List<Client> clientsList = new ArrayList<>(); try (Connection con = dataSourceClient.getConnection(); PreparedStatement ps = con.prepareStatement(query); ResultSet rs = ps.executeQuery();) { while (rs.next()) { Client client = new Client(); client.setId(rs.getInt(1));/*from w w w. ja va 2 s. co m*/ client.setNume(rs.getString(2)); client.setPrenume(rs.getString(3)); client.setNrTelefon(rs.getInt(4)); client.setEmail(rs.getString(5)); client.setDateExpirare(rs.getTimestamp(6)); clientsList.add(client); } } catch (SQLException e) { e.printStackTrace(); } return clientsList; }
From source file:com.wms.multitenant.tenant.provider.MultiTenantConnectionProviderImpl.java
private DataSource constructDataSource(String dbName) { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(// w w w . ja va 2 s .c om springEnvironment.getProperty("tenant.datasource.classname", "com.mysql.jdbc.Driver")); dataSource.setUrl(springEnvironment.getProperty("tenant.datasource.url", "jdbc:mysql://localhost:3306/") + dbName + "?createDatabaseIfNotExist=true"); dataSource.setUsername(springEnvironment.getProperty("tenant.datasource.user", "root")); dataSource.setPassword(springEnvironment.getProperty("tenant.datasource.password", "root")); // ResourceDatabasePopulator rdp = new ResourceDatabasePopulator(); // rdp.populate(dataSource.getConnection()); try { dataSource.getConnection().createStatement().execute("CREATE DATABASE IF NOT EXISTS " + dbName); dataSource.getConnection().createStatement().execute("CREATE TABLE IF NOT EXISTS `product` (\n" + " `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(128) NOT NULL,\n" + " `product_id` varchar(128) DEFAULT NULL,\n" + " `price` double DEFAULT NULL,\n" + " `description` varchar(256) DEFAULT NULL,\n" + " `created` timestamp NULL DEFAULT NULL,\n" + " `updated` timestamp NULL DEFAULT NULL,\n" + " `deleted` timestamp NULL DEFAULT NULL,\n" + " PRIMARY KEY (`id`),\n" + " UNIQUE KEY `name` (`name`)\n" + ") ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;"); } catch (Exception ex) { System.out.println(ex); } return dataSource; }
From source file:org.unbunt.ella.Ella.java
protected ELLA prepareELLA() throws EllaIOException, DBConnectionFailedException, DataSourceInitializationException, DatabaseException { ELLA ella = createELLA();// w w w . j a v a2 s.c om Context context = ella.getContext(); context.setInputStream(inputStream); context.setOutputStream(new PrintStream(outputStream, true)); context.setErrorStream(new PrintStream(errorStream, true)); context.setLogLevel(logLevel); if (logOutput) { Logger ellaLogger = LoggerFactory.getLogger("ella"); if (logOnly) { context.setLogger(new SLF4JContextLogger(ellaLogger)); } else { context.setLogger(new DupContextLogger( new PrintStreamLogger(context.getOutputStream(), context.getErrorStream()), new SLF4JContextLogger(ellaLogger))); } SLF4JOutputStream slf4jOutputStream = new SLF4JOutputStream(ellaLogger, SLF4JOutputStream.Priority.info); SLF4JOutputStream slf4jErrorStream = new SLF4JOutputStream(ellaLogger, SLF4JOutputStream.Priority.warn); OutputStream outputStream = logOnly ? slf4jOutputStream : new DupOutputStream(new OutputStream[] { context.getOutputStream(), slf4jOutputStream }); OutputStream errorStream = logOnly ? slf4jErrorStream : new DupOutputStream(new OutputStream[] { context.getErrorStream(), slf4jErrorStream }); context.setOutputStream(new PrintStream(outputStream)); context.setErrorStream(new PrintStream(errorStream)); } DriverManagerDataSource ds = null; if (connectionDetails != null) { ds = connectionDetails.createDataSource(); } currentConnection = null; if (ds != null) { try { currentConnection = ds.getConnection(); } catch (SQLException e) { throw new DBConnectionFailedException(e.getMessage(), e); } } currentBatch = null; if (currentConnection != null) { if (batchStatements) { try { currentBatch = new StmtBatch(context, currentConnection, batchSize); } catch (SQLException e) { throw new DatabaseException("Failed to activate batch processing: " + e.getMessage(), e); } finally { try { currentConnection.close(); } catch (Exception ignored) { } } context.getObjConnMgr().activate(currentConnection, currentBatch); } else { context.getObjConnMgr().activate(currentConnection); } } context.addSQLResultListener(new SimpleSQLResultListener(context.getOutputStream())); return ella; }
From source file:org.finra.herd.service.impl.RelationalTableRegistrationHelperServiceImpl.java
/** * Retrieves a list of actual schema columns for the specified relational table. This method uses actual JDBC connection to retrieve a description of table * columns.// w w w .j a va2 s . co m * * @param relationalStorageAttributesDto the relational storage attributes DTO * @param relationalSchemaName the name of the relational database schema * @param relationalTableName the name of the relational table * * @return the list of schema columns for the specified relational table */ List<SchemaColumn> retrieveRelationalTableColumnsImpl( RelationalStorageAttributesDto relationalStorageAttributesDto, String relationalSchemaName, String relationalTableName) { // Get the JDBC password value. String password = getPassword(relationalStorageAttributesDto); // Create and initialize a driver manager data source (a simple implementation of the standard JDBC interface). // We only support PostgreSQL database type. DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setUrl(relationalStorageAttributesDto.getJdbcUrl()); driverManagerDataSource.setUsername(relationalStorageAttributesDto.getJdbcUsername()); driverManagerDataSource.setPassword(password); driverManagerDataSource.setDriverClassName(JdbcServiceImpl.DRIVER_POSTGRES); // Create an empty result list. List<SchemaColumn> schemaColumns = new ArrayList<>(); // Connect to the database and retrieve the relational table columns. try (Connection connection = driverManagerDataSource.getConnection()) { DatabaseMetaData databaseMetaData = connection.getMetaData(); // Check if the specified relational table exists in the database. try (ResultSet tables = databaseMetaData.getTables(null, relationalSchemaName, relationalTableName, null)) { Assert.isTrue(tables.next(), String.format( "Relational table with \"%s\" name not found under \"%s\" schema at jdbc.url=\"%s\" for jdbc.username=\"%s\".", relationalTableName, relationalSchemaName, driverManagerDataSource.getUrl(), driverManagerDataSource.getUsername())); } // Retrieve the relational table columns. try (ResultSet columns = databaseMetaData.getColumns(null, relationalSchemaName, relationalTableName, null)) { while (columns.next()) { SchemaColumn schemaColumn = new SchemaColumn(); schemaColumn.setName(columns.getString("COLUMN_NAME")); schemaColumn.setType(columns.getString("TYPE_NAME")); schemaColumn.setSize(columns.getString("COLUMN_SIZE")); schemaColumn.setRequired(columns.getInt("NULLABLE") == 0); schemaColumn.setDefaultValue(columns.getString("COLUMN_DEF")); schemaColumns.add(schemaColumn); } } } catch (SQLException e) { throw new IllegalArgumentException(String.format( "Failed to retrieve description of a relational table with \"%s\" name under \"%s\" schema " + "at jdbc.url=\"%s\" using jdbc.username=\"%s\". Reason: %s", relationalTableName, relationalSchemaName, driverManagerDataSource.getUrl(), driverManagerDataSource.getUsername(), e.getMessage()), e); } return schemaColumns; }
From source file:org.mifos.framework.util.DbUnitUtilities.java
public void dumpDatabase(String fileName, DriverManagerDataSource dataSource) throws SQLException, FileNotFoundException, DatabaseUnitException, IOException { Connection jdbcConnection = null; try {/* w ww. jav a 2 s. co m*/ jdbcConnection = dataSource.getConnection(); this.dumpDatabase(fileName, jdbcConnection); } finally { jdbcConnection.close(); } }