List of usage examples for java.sql DatabaseMetaData getDriverVersion
String getDriverVersion() throws SQLException;
String
. From source file:org.springframework.jdbc.core.JdbcTemplate.java
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException { if (logger.isDebugEnabled()) { logger.debug("Executing SQL batch update [" + sql + "]"); }/*from ww w . jav a 2 s . co m*/ return (int[]) execute(sql, new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement ps) throws SQLException { int batchSize = pss.getBatchSize(); DatabaseMetaData dbmd = ps.getConnection().getMetaData(); try { boolean supportsBatchUpdates = false; try { if (dbmd != null) { if (dbmd.supportsBatchUpdates()) { if (logger.isDebugEnabled()) { logger.debug("Batch Updates supported for [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } supportsBatchUpdates = true; } else { if (logger.isDebugEnabled()) { logger.debug("Batch Updates are not supported for [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } } } } catch (AbstractMethodError ame) { logger.warn("Driver does not support JDBC 2.0 method supportsBatchUpdatres [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } if (supportsBatchUpdates) { for (int i = 0; i < batchSize; i++) { pss.setValues(ps, i); ps.addBatch(); } return ps.executeBatch(); } else { int[] rowsAffected = new int[batchSize]; for (int i = 0; i < batchSize; i++) { pss.setValues(ps, i); rowsAffected[i] = ps.executeUpdate(); } return rowsAffected; } } finally { if (pss instanceof ParameterDisposer) { ((ParameterDisposer) pss).cleanupParameters(); } } } }); }
From source file:org.wso2.carbon.server.admin.service.ServerAdmin.java
public ServerData getServerData() throws Exception { boolean isRestricted = true; MessageContext msgContext = MessageContext.getCurrentMessageContext(); if (msgContext != null) { HttpServletRequest request = (HttpServletRequest) msgContext .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); HttpSession httpSession = request.getSession(false); if (httpSession != null) { String userName = (String) httpSession.getAttribute(ServerConstants.USER_LOGGED_IN); isRestricted = !getUserRealm().getAuthorizationManager().isUserAuthorized(userName, "/permission/protected/server-admin/homepage", CarbonConstants.UI_PERMISSION_ACTION); }/* w ww . j av a2s .c o m*/ } else { // non SOAP call isRestricted = false; } String location = null; if (!isRestricted) { location = getAxisConfig().getRepository().toString(); } ServerData data = new ServerData(ServerConstants.SERVER_NAME, location, (getTenantDomain() != null && !getTenantDomain().equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)), isRestricted); if (!isRestricted) { Parameter systemStartTime = getAxisConfig().getParameter(CarbonConstants.SERVER_START_TIME); long startTime = 0; if (systemStartTime != null) { startTime = Long.parseLong((String) systemStartTime.getValue()); } Date stTime = new Date(startTime); SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"); data.setServerStartTime(dateFormatter.format(stTime)); data.setServerUpTime(getTime((System.currentTimeMillis() - startTime) / 1000)); Parameter systemStartUpDuration = getAxisConfig().getParameter(CarbonConstants.START_UP_DURATION); if (systemStartUpDuration != null) { data.setServerStartUpDuration((String) systemStartUpDuration.getValue()); } ServerConfigurationService serverConfig = dataHolder.getServerConfig(); String registryType = serverConfig.getFirstProperty("Registry.Type"); if (registryType == null) { registryType = "embedded"; } data.setRegistryType(registryType); // Extract DB related data from RegistryContext if (registryType.equals("embedded")) { try { DataAccessManager dataAccessManager = RegistryContext.getBaseInstance().getDataAccessManager(); if (!(dataAccessManager instanceof JDBCDataAccessManager)) { String msg = "Failed to obtain DB connection. Invalid data access manager."; log.error(msg); throw new AxisFault(msg); } DataSource dataSource = ((JDBCDataAccessManager) dataAccessManager).getDataSource(); Connection dbConnection = dataSource.getConnection(); DatabaseMetaData metaData = dbConnection.getMetaData(); if (metaData != null) { data.setDbName(metaData.getDatabaseProductName()); data.setDbVersion(metaData.getDatabaseProductVersion()); data.setDbDriverName(metaData.getDriverName()); data.setDbDriverVersion(metaData.getDriverVersion()); data.setDbURL(metaData.getURL()); } dbConnection.close(); } catch (SQLException e) { String msg = "Cannot create DB connection"; log.error(msg, e); throw new AxisFault(msg, e); } } else if (registryType.equals("remote")) { data.setRemoteRegistryChroot(serverConfig.getFirstProperty("Registry.Chroot")); data.setRemoteRegistryURL(serverConfig.getFirstProperty("Registry.Url")); } } try { data.setServerIp(NetworkUtils.getLocalHostname()); } catch (SocketException e) { throw new AxisFault(e.getMessage(), e); } return data; }
From source file:ro.nextreports.designer.dbviewer.DefaultDBViewer.java
public DBInfo getDBInfo(String schemaName, int mask, Connection con) throws NextSqlException { String info = ""; List<String> keywords = new ArrayList<String>(); List<DBTable> tables = new ArrayList<DBTable>(); List<DBProcedure> procedures = new ArrayList<DBProcedure>(); Dialect dialect;//w w w . jav a 2 s . c o m try { dialect = DialectUtil.getDialect(con); } catch (Exception ex) { ex.printStackTrace(); throw new NextSqlException("Could not get Dialect.", ex); } try { DatabaseMetaData dbmd = con.getMetaData(); if ((mask & DBInfo.INFO) == DBInfo.INFO) { StringBuffer sb = new StringBuffer(); sb.append(I18NSupport.getString("database.product")).append(dbmd.getDatabaseProductName()) .append("\r\n"); sb.append(I18NSupport.getString("database.product.version")) .append(dbmd.getDatabaseProductVersion()).append("\r\n"); sb.append(I18NSupport.getString("database.driver.name")).append(dbmd.getDriverName()) .append("\r\n"); sb.append(I18NSupport.getString("database.driver.version")).append(dbmd.getDriverVersion()) .append("\r\n"); info = sb.toString(); } if ((mask & DBInfo.SUPPORTED_KEYWORDS) == DBInfo.SUPPORTED_KEYWORDS) { StringTokenizer st = new StringTokenizer(dbmd.getSQLKeywords(), ","); while (st.hasMoreTokens()) { keywords.add(st.nextToken()); } } // Get a ResultSet that contains all of the tables in this database // We specify a table_type of "TABLE" to prevent seeing system tables, // views and so forth boolean tableMask = ((mask & DBInfo.TABLES) == DBInfo.TABLES); boolean viewMask = ((mask & DBInfo.VIEWS) == DBInfo.VIEWS); if (tableMask || viewMask) { String[] tableTypes; if (tableMask && viewMask) { tableTypes = new String[] { "TABLE", "VIEW" }; } else if (tableMask) { tableTypes = new String[] { "TABLE" }; } else { tableTypes = new String[] { "VIEW" }; } String pattern = tableMask ? Globals.getTableNamePattern() : Globals.getViewNamePattern(); ResultSet allTables = dbmd.getTables(null, schemaName, pattern, tableTypes); try { while (allTables.next()) { String table_name = allTables.getString("TABLE_NAME"); String table_type = allTables.getString("TABLE_TYPE"); // discard recycle bin tables String ignoreTablePrefix = dialect.getRecycleBinTablePrefix(); if ((table_name == null) || ((ignoreTablePrefix != null) && table_name.startsWith(ignoreTablePrefix))) { continue; } if ((mask & DBInfo.INDEXES) == DBInfo.INDEXES) { ResultSet indexList = null; try { // Get a list of all the indexes for this table indexList = dbmd.getIndexInfo(null, schemaName, table_name, false, false); List<DBIndex> indexes = new ArrayList<DBIndex>(); while (indexList.next()) { String index_name = indexList.getString("INDEX_NAME"); String column_name = indexList.getString("COLUMN_NAME"); if (!index_name.equals("null")) { DBIndex index = new DBIndex(index_name, column_name); indexes.add(index); } } DBTable table = new DBTable(schemaName, table_name, table_type, indexes); tables.add(table); } catch (SQLException e) { throw new NextSqlException("SQL Exception: " + e.getMessage(), e); } finally { closeResultSet(indexList); } } else { DBTable table = new DBTable(schemaName, table_name, table_type); tables.add(table); } } } catch (SQLException e) { throw new NextSqlException("SQL Exception: " + e.getMessage(), e); } finally { closeResultSet(allTables); } } boolean procedureMask = ((mask & DBInfo.PROCEDURES) == DBInfo.PROCEDURES); if (procedureMask) { String pattern = Globals.getProcedureNamePattern(); if (pattern == null) { pattern = "%"; } ResultSet rs = dbmd.getProcedures(null, schemaName, pattern); try { while (rs.next()) { String spName = rs.getString("PROCEDURE_NAME"); int spType = rs.getInt("PROCEDURE_TYPE"); String catalog = rs.getString("PROCEDURE_CAT"); // System.out.println("Stored Procedure Name: " + spName); // if (spType == DatabaseMetaData.procedureReturnsResult) { // System.out.println("procedure Returns Result"); // } else if (spType == DatabaseMetaData.procedureNoResult) { // System.out.println("procedure No Result"); // } else { // System.out.println("procedure Result unknown"); // } procedures.add(new DBProcedure(schemaName, catalog, spName, spType)); } } catch (SQLException e) { throw new NextSqlException("SQL Exception: " + e.getMessage(), e); } finally { closeResultSet(rs); } } } catch (SQLException e) { LOG.error(e.getMessage(), e); e.printStackTrace(); throw new NextSqlException("SQL Exception: " + e.getMessage(), e); } return new DBInfo(info, tables, procedures, keywords); }