List of usage examples for java.sql SQLException toString
public String toString()
From source file:com.zimbra.cs.db.DebugPreparedStatement.java
private void logException(SQLException e) { if (ZimbraLog.sqltrace.isDebugEnabled()) { ZimbraLog.sqltrace.debug(e.toString() + ": " + getSql() + getHashCodeString()); }// w w w .j a v a 2 s . c om }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public List<Picture> findAllPicture() { List<Picture> list = null; final String query = "SELECT * FROM picture"; Connection connection = this.databaseSpring.connect(); try {//from w ww .j a v a 2 s . c o m PreparedStatement preStat = connection.prepareStatement(query); try { ResultSet resultSet = preStat.executeQuery(); list = new ArrayList<Picture>(); try { while (resultSet.next()) { list.add(this.loadPictureFromResultSet(resultSet)); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.WARN, e.toString()); } } return list; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public Picture findPictureByKey(String id) { Picture result = null;//from ww w. ja v a2 s .c o m final String query = "SELECT * FROM picture WHERE id = ?"; Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query); try { preStat.setString(1, id); ResultSet resultSet = preStat.executeQuery(); try { if (resultSet.first()) { result = loadPictureFromResultSet(resultSet); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.WARN, e.toString()); } } return result; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public List<Picture> findPicturePerClause(String whereClause) { List<Picture> list = null; StringBuilder query = new StringBuilder(); query.append("SELECT * FROM picture where 1=1 "); query.append(whereClause);/* w w w . ja v a 2s .c o m*/ Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query.toString()); try { ResultSet resultSet = preStat.executeQuery(); list = new ArrayList<Picture>(); try { while (resultSet.next()) { list.add(this.loadPictureFromResultSet(resultSet)); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.WARN, e.toString()); } } return list; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public List<String> findDistinctValuesfromColumn(String colName) { List<String> result = new ArrayList<String>(); StringBuilder query = new StringBuilder(); query.append("SELECT distinct "); query.append(colName);/*from w w w . j a v a2 s . c om*/ query.append(" FROM picture order by "); query.append(colName); query.append(" asc"); Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query.toString()); try { ResultSet resultSet = preStat.executeQuery(); try { while (resultSet.next()) { result.add(resultSet.getString(1) == null ? "" : resultSet.getString(1)); } resultSet.close(); } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { Logger.log(PictureDAO.class.getName(), Level.INFO, "Disconnecting to jdbc/qualityfollowup from findDistinctValuesfromParameter"); connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.WARN, e.toString()); } } return result; }
From source file:eu.celarcloud.celar_ms.ServerPack.Database.MySQL.DBHandlerWithConnPool.java
public void dbInit(boolean drop_tables) throws CatascopiaException { Connection c = null;/*from w ww . ja va 2s.c om*/ String query = ""; PreparedStatement stmt = null; try { c = this.getConnection(); if (drop_tables) { final String[] tables = { "agent_table", "metric_table", "metric_value_table", "subscription_table", "subscription_agents_table" }; for (String table : tables) { query = "DROP TABLE IF EXISTS " + table; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> dropped table: " + table); } } query = "CREATE TABLE IF NOT EXISTS `agent_table` (" + "`agentID` varchar(32) NOT NULL," + "`agentIP` varchar(16) NOT NULL," + "`agentName` varchar(16) NOT NULL," + "`status` enum('UP','DOWN','TERMINATED') NOT NULL," + "`tstart` timestamp DEFAULT CURRENT_TIMESTAMP," + "`tstop` timestamp NULL," + "`tags` varchar(64) NULL," + "PRIMARY KEY (`agentID`)" + ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> created table: agent_table"); query = "CREATE TABLE IF NOT EXISTS `metric_table` (" + "`metricID` varchar(64) NOT NULL," + "`agentID` varchar(32) NOT NULL," + "`name` varchar(50) NOT NULL," + "`mgroup` varchar(50) NOT NULL," + "`units` varchar(10) NOT NULL," + "`type` varchar(20) NOT NULL," + "`is_sub` varchar(10) DEFAULT NULL," + "PRIMARY KEY (`metricID`)" + ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> created table: metric_table"); // query = "CREATE TABLE IF NOT EXISTS `metric_value_table` (" + // "`valueID` bigint(20) unsigned NOT NULL AUTO_INCREMENT," + // "`metricID` varchar(64) NOT NULL," + // "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + // "`value` varchar(32) NOT NULL," + // "PRIMARY KEY (`valueID`)) " + // "ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"; query = "CREATE TABLE IF NOT EXISTS `metric_value_table` (" + "`valueID` bigint(20) unsigned NOT NULL AUTO_INCREMENT," + "`metricID` varchar(64) NOT NULL," + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + "`value` varchar(32) NOT NULL," + "`name` varchar(50) NOT NULL," + "`mgroup` varchar(50) NOT NULL," + "`units` varchar(10) NOT NULL," + "`type` varchar(20) NOT NULL," + "PRIMARY KEY (`valueID`)) " + "ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> created table: metric_value_table"); query = "CREATE TABLE IF NOT EXISTS `subscription_table` (" + "`subID` varchar(32) NOT NULL, " + "`func` varchar(256) NOT NULL, " + "`period` int(11) NOT NULL, " + "`originMetric` varchar(50) NOT NULL," + "PRIMARY KEY (`subID`)) " + "ENGINE=InnoDB DEFAULT CHARSET=latin1;"; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> created table: subscription_table"); query = "CREATE TABLE IF NOT EXISTS `subscription_agents_table` (" + " `subID` varchar(32) NOT NULL," + " `agentID` varchar(32) NOT NULL," + " PRIMARY KEY (`subID`,`agentID`)) " + "ENGINE=InnoDB DEFAULT CHARSET=latin1;"; stmt = c.prepareStatement(query); stmt.executeUpdate(); server.writeToLog(Level.INFO, "MySQL DBHandler>> created table: subscription_agents_table"); } catch (SQLException e) { throw new CatascopiaException(e.toString(), CatascopiaException.ExceptionType.DATABASE); } catch (Exception e) { throw new CatascopiaException(e.toString(), CatascopiaException.ExceptionType.DATABASE); } finally { if (stmt != null) try { stmt.close(); } catch (SQLException e) { throw new CatascopiaException(e.toString(), CatascopiaException.ExceptionType.DATABASE); } } }
From source file:com.strider.datadefender.DatabaseDiscoverer.java
private List<MatchMetaData> discoverAgainstSingleModel(final IDBFactory factory, final Properties dataDiscoveryProperties, final Model model, final double probabilityThreshold, final String vendor) throws ParseException, DatabaseDiscoveryException { final IMetaData metaData = factory.fetchMetaData(); final List<MatchMetaData> map = metaData.getMetaData(vendor); // Start running NLP algorithms for each column and collect percentage matches = new ArrayList<>(); MatchMetaData specialCaseData = null; final List<MatchMetaData> specialCaseDataList = new ArrayList(); boolean specialCase = false; final String extentionList = dataDiscoveryProperties.getProperty("extentions"); String[] specialCaseFunctions = null; log.info("Extention list: " + extentionList); if (!CommonUtils.isEmptyString(extentionList)) { specialCaseFunctions = extentionList.split(","); if ((specialCaseFunctions != null) && (specialCaseFunctions.length > 0)) { specialCase = true;/*from w w w . ja va 2 s.com*/ } } final ISQLBuilder sqlBuilder = factory.createSQLBuilder(); List<Probability> probabilityList; for (final MatchMetaData data : map) { final String tableName = data.getTableName(); final String columnName = data.getColumnName(); log.info(data.getPkeys().toString()); if (data.getPkeys().contains(columnName.toLowerCase(Locale.ENGLISH))) { log.info("Column [" + columnName + "] is Primary Key. Slipping this column."); continue; } log.info(data.getFkeys().toString()); if (data.getFkeys().contains(columnName.toLowerCase(Locale.ENGLISH))) { log.info("Column [" + columnName + "] is Foreign Key. Slipping this column."); continue; } log.debug("Column type: [" + data.getColumnType() + "]"); probabilityList = new ArrayList<>(); log.info("Analyzing column [" + tableName + "].[" + columnName + "]"); final String tableNamePattern = dataDiscoveryProperties.getProperty("table_name_pattern"); if (!CommonUtils.isEmptyString(tableNamePattern)) { final Pattern p = compile(tableNamePattern); if (!p.matcher(tableName).matches()) { continue; } } final String table = sqlBuilder.prefixSchema(tableName); final int limit = Integer.parseInt(dataDiscoveryProperties.getProperty("limit")); final String query = sqlBuilder.buildSelectWithLimit( "SELECT " + columnName + " FROM " + table + " WHERE " + columnName + " IS NOT NULL ", limit); log.debug("Executing query against database: " + query); try (Statement stmt = factory.getConnection().createStatement(); ResultSet resultSet = stmt.executeQuery(query);) { while (resultSet.next()) { if (data.getColumnType().equals("BLOB") || data.getColumnType().equals("GEOMETRY")) { continue; } if (model.getName().equals("location") && data.getColumnType().contains("INT")) { continue; } final String sentence = resultSet.getString(1); log.debug(sentence); log.debug("special case:" + specialCase); if (specialCase) { try { for (int i = 0; i < specialCaseFunctions.length; i++) { if ((sentence != null) && !sentence.isEmpty()) { log.debug("sentence: " + sentence); log.debug("data: " + data); specialCaseData = (MatchMetaData) callExtention(specialCaseFunctions[i], data, sentence); if (specialCaseData != null) { log.info("Adding new special case data: " + specialCaseData.toString()); specialCaseDataList.add(specialCaseData); } else { log.debug("No special case data found"); } } } } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { log.error(e.toString()); } } if ((sentence != null) && !sentence.isEmpty()) { String processingValue = ""; if (data.getColumnType().equals("DATE") || data.getColumnType().equals("TIMESTAMP") || data.getColumnType().equals("DATETIME")) { final DateFormat originalFormat = new SimpleDateFormat(sentence, Locale.ENGLISH); final DateFormat targetFormat = new SimpleDateFormat("MMM d, yy", Locale.ENGLISH); final java.util.Date date = originalFormat.parse(sentence); processingValue = targetFormat.format(date); } else { processingValue = sentence; } // log.debug(sentence); // Convert sentence into tokens final String tokens[] = model.getTokenizer().tokenize(processingValue); // Find names final Span nameSpans[] = model.getNameFinder().find(tokens); // find probabilities for names final double[] spanProbs = model.getNameFinder().probs(nameSpans); // Collect top X tokens with highest probability // display names for (int i = 0; i < nameSpans.length; i++) { final String span = nameSpans[i].toString(); if (span.length() > 2) { log.debug("Span: " + span); log.debug("Covered text is: " + tokens[nameSpans[i].getStart()]); log.debug("Probability is: " + spanProbs[i]); probabilityList.add(new Probability(tokens[nameSpans[i].getStart()], spanProbs[i])); } } // From OpenNLP documentation: // After every document clearAdaptiveData must be called to clear the adaptive data in the feature generators. // Not calling clearAdaptiveData can lead to a sharp drop in the detection rate after a few documents. model.getNameFinder().clearAdaptiveData(); } } } catch (SQLException sqle) { log.error(sqle.toString()); } final double averageProbability = calculateAverage(probabilityList); if (averageProbability >= probabilityThreshold) { data.setAverageProbability(averageProbability); data.setModel(model.getName()); data.setProbabilityList(probabilityList); matches.add(data); } } // Special processing if ((specialCaseDataList != null) && !specialCaseDataList.isEmpty()) { log.info("Special case data is processed :" + specialCaseDataList.toString()); for (final MatchMetaData specialData : specialCaseDataList) { matches.add(specialData); } } return matches; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public List<Picture> findPictureListByCriteria(String individualSearch, String joinedSearch) { List<Picture> pictureList = new ArrayList<Picture>(); StringBuilder searchSQL = new StringBuilder(); StringBuilder searchSQL2 = new StringBuilder(); StringBuilder query = new StringBuilder(); query.append("SELECT p.id, p.application, p.page, p.picture, '' as base64, p.localpath FROM picture p "); if (!joinedSearch.equals("")) { query.append(//w ww.j a v a 2s .com " join datamap d on p.page=d.page and p.picture=d.picture and p.application=d.application"); } query.append(" where 1=1 "); if (!joinedSearch.equals("")) { searchSQL.append(joinedSearch); } if (!individualSearch.equals("")) { if (!individualSearch.startsWith(" AND ")) { individualSearch = " AND " + individualSearch; } searchSQL.append(individualSearch); } query.append(searchSQL2); query.append(searchSQL); query.append(" group by p.page,p.picture "); Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query.toString()); try { ResultSet resultSet = preStat.executeQuery(); try { while (resultSet.next()) { pictureList.add(this.loadPictureFromResultSet(resultSet)); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.ERROR, e.toString()); } } return pictureList; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public Integer getNumberOfPicturePerCrtiteria(String searchTerm, String inds) { Integer result = 0;//from ww w .j a v a2 s.co m StringBuilder query = new StringBuilder(); StringBuilder gSearch = new StringBuilder(); String searchSQL = ""; query.append("SELECT count(*) FROM picture"); gSearch.append(" where (`id` like '%"); gSearch.append(searchTerm); gSearch.append("%'"); gSearch.append(" or `page` like '%"); gSearch.append(searchTerm); gSearch.append("%'"); gSearch.append(" or `application` like '%"); gSearch.append(searchTerm); gSearch.append("%'"); gSearch.append(" or `picture` like '%"); gSearch.append(searchTerm); gSearch.append("%')"); if (!searchTerm.equals("") && !inds.equals("")) { searchSQL = gSearch.toString() + " and " + inds; } else if (!inds.equals("")) { searchSQL = " where " + inds; } else if (!searchTerm.equals("")) { searchSQL = gSearch.toString(); } query.append(searchSQL); Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query.toString()); try { ResultSet resultSet = preStat.executeQuery(); try { if (resultSet.first()) { result = resultSet.getInt(1); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.ERROR, e.toString()); } } return result; }
From source file:org.apache.openaz.xacml.std.pip.engines.jdbc.ConfigurableJDBCResolver.java
@Override public PreparedStatement getPreparedStatement(PIPEngine pipEngine, PIPRequest pipRequest, PIPFinder pipFinder, Connection connection) throws PIPException { /*//from ww w.j av a 2 s.co m * Do we support the request? */ if (!this.isSupported(pipRequest)) { return null; } PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(this.sqlQuery); } catch (SQLException ex) { this.logger.error("SQLException creating PreparedStatement: " + ex.toString(), ex); // TODO: throw the exception or return a null PreparedStatement? return null; } if (this.parameters.size() > 0) { /* * Gather all of the AttributeValues for parameters to the prepared statement. For now, we assume * a single value for each parameter. If there are multiple values we will log an error and return * a null PreparedStatement. TODO: Should the interface change to return a cross-product of * PreparedStatements to deal with multiple values for parameters? If not, should we just take the * first value and use it as the parameter value? */ for (int i = 0; i < this.parameters.size(); i++) { PIPRequest pipRequestParameter = this.parameters.get(i); PIPResponse pipResponse = pipFinder.getMatchingAttributes(pipRequestParameter, null); if (pipResponse.getStatus() == null || pipResponse.getStatus().isOk()) { Collection<Attribute> listAttributes = pipResponse.getAttributes(); if (listAttributes.size() > 0) { if (listAttributes.size() > 1) { this.logger.error("PIPFinder returned more than one Attribute for " + pipRequestParameter.toString()); throw new PIPException("PIPFinder returned more than one Attribute for " + pipRequestParameter.toString()); } Collection<AttributeValue<?>> listAttributeValuesReturned = listAttributes.iterator().next() .getValues(); if (listAttributeValuesReturned.size() > 0) { if (listAttributeValuesReturned.size() > 1) { this.logger.warn("PIPFinder returned more than one AttributeValue for " + pipRequestParameter.toString()); return null; } AttributeValue<?> attributeValue = listAttributeValuesReturned.iterator().next(); Identifier identifierAttributeValueDataType = attributeValue.getDataTypeId(); try { if (identifierAttributeValueDataType.equals(XACML3.ID_DATATYPE_INTEGER)) { preparedStatement.setInt(i + 1, DataTypes.DT_INTEGER.convert(attributeValue.getValue()).intValue()); } else if (identifierAttributeValueDataType.equals(XACML3.ID_DATATYPE_DOUBLE)) { preparedStatement.setDouble(i + 1, DataTypes.DT_DOUBLE.convert(attributeValue.getValue())); } else if (identifierAttributeValueDataType.equals(XACML3.ID_DATATYPE_BOOLEAN)) { preparedStatement.setBoolean(i + 1, DataTypes.DT_BOOLEAN.convert(attributeValue.getValue())); } else if (identifierAttributeValueDataType.equals(XACML3.ID_DATATYPE_DATETIME)) { ISO8601DateTime iso8601DateTime = DataTypes.DT_DATETIME .convert(attributeValue.getValue()); java.sql.Date sqlDate = new java.sql.Date( iso8601DateTime.getCalendar().getTimeInMillis()); preparedStatement.setDate(i + 1, sqlDate, iso8601DateTime.getCalendar()); } else if (identifierAttributeValueDataType.equals(XACML3.ID_DATATYPE_DATE)) { ISO8601Date iso8601Date = DataTypes.DT_DATE.convert(attributeValue.getValue()); java.sql.Date sqlDate = new java.sql.Date( iso8601Date.getCalendar().getTimeInMillis()); preparedStatement.setDate(i + 1, sqlDate, iso8601Date.getCalendar()); } else { preparedStatement.setString(i + 1, DataTypes.DT_STRING.convert(attributeValue.getValue())); } } catch (Exception ex) { this.logger.error("Exception setting parameter " + (i + 1) + " to " + attributeValue.toString() + ": " + ex.toString(), ex); return null; } } else { this.logger.warn( "No AttributeValues returned for parameter " + pipRequestParameter.toString()); return null; } } else { this.logger.warn("No Attributes returned for parameter " + pipRequestParameter.toString()); return null; } } else { this.logger.warn("PIPFinder returned status " + pipResponse.getStatus().toString()); return null; } } } return preparedStatement; }