List of usage examples for java.sql ResultSet getRow
int getRow() throws SQLException;
From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBWriter.java
/** Checks to see if the security exists in the securities table. If it does, return its ID number. If it * does not, add it to the securities table and return its ID number */ private int addSecurity(String security) { Connection conn = null;//w ww .j av a2 s . co m Object[] results = null; int id = -1; try { conn = dbc.getConnection(); String query = "select id from securities where security_name='" + security + "'"; results = dbc.executeQuery(query, conn); //Check if the security is there -- if so, return its id if (results != null) { ResultSet rs = (ResultSet) results[0]; if (!rs.wasNull()) { rs.last(); int size = rs.getRow(); if (size > 0) { return rs.getInt("id"); } } } //dbc.closeQuery(results); //If the security was not found then add it and return the generated id String update = "insert into securities values(0, '" + security + "')"; results = dbc.executeUpdate(update, conn); ResultSet rs = (ResultSet) results[0]; rs.next(); id = rs.getInt(1); } catch (SQLException e) { log.error("Failed to add security " + security + " to securities table", e); return -1; } finally { dbc.closeQuery(results, conn); } return id; }
From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBWriter.java
/** Checks to see if the group exists in the market_groups table. If it does, return its ID number. If it * does not, add it to the market_groups table and return its ID number */ private int addGroup(String group) { Connection conn = null;/*from ww w . ja va 2s .c om*/ Object[] results = null; int id = -1; try { conn = dbc.getConnection(); String query = "select id from market_groups where group_name='" + group + "'"; results = dbc.executeQuery(query, conn); //Check if the security is there -- if so, return its id if (results != null) { ResultSet rs = (ResultSet) results[0]; if (!rs.wasNull()) { rs.last(); int size = rs.getRow(); if (size > 0) { id = rs.getInt("id"); dbc.closeQuery(results); return id; } } } dbc.closeQuery(results); //If the group was not found then add it and return the generated id String update = "insert into market_groups values(0, '" + group + "')"; results = dbc.executeUpdate(update, conn); ResultSet rs = (ResultSet) results[0]; rs.next(); return rs.getInt(1); } catch (SQLException e) { log.error("Failed to add group " + group + " to market_groups table", e); } finally { dbc.closeQuery(results, conn); } return id; }
From source file:com.micromux.cassandra.jdbc.JdbcRegressionTest.java
/** * Create a column group and confirm that the {@code ResultSetMetaData} works. *///ww w . ja va 2s .c om @Test public void testResultSetMetaData() throws Exception { Statement stmt = con.createStatement(); // Create the target Column family String createCF = "CREATE COLUMNFAMILY t33 (k int PRIMARY KEY," + "c text " + ") ;"; stmt.execute(createCF); stmt.close(); con.close(); // open it up again to see the new CF con = DriverManager .getConnection(String.format("jdbc:cassandra://%s:%d/%s?%s", HOST, PORT, KEYSPACE, OPTIONS)); // paraphrase of the snippet from the ISSUE #33 provided test PreparedStatement statement = con.prepareStatement("update t33 set c=? where k=123"); statement.setString(1, "mark"); statement.executeUpdate(); ResultSet result = statement.executeQuery("SELECT k, c FROM t33;"); ResultSetMetaData metadata = result.getMetaData(); int colCount = metadata.getColumnCount(); System.out.println("Test Issue #33"); DatabaseMetaData md = con.getMetaData(); System.out.println(); System.out.println("--------------"); System.out.println("Driver Version : " + md.getDriverVersion()); System.out.println("DB Version : " + md.getDatabaseProductVersion()); System.out.println("Catalog term : " + md.getCatalogTerm()); System.out.println("Catalog : " + con.getCatalog()); System.out.println("Schema term : " + md.getSchemaTerm()); System.out.println("--------------"); while (result.next()) { metadata = result.getMetaData(); colCount = metadata.getColumnCount(); assertEquals("Total column count should match schema for t33", 2, metadata.getColumnCount()); System.out.printf("(%d) ", result.getRow()); for (int i = 1; i <= colCount; i++) { System.out.print(showColumn(i, result) + " "); switch (i) { case 1: assertEquals("First Column: k", "k", metadata.getColumnName(1)); assertEquals("First Column Type: int", Types.INTEGER, metadata.getColumnType(1)); break; case 2: assertEquals("Second Column: c", "c", metadata.getColumnName(2)); assertEquals("Second Column Type: text", Types.NVARCHAR, metadata.getColumnType(2)); break; } } System.out.println(); } }
From source file:mom.trd.opentheso.bdd.helper.FacetHelper.java
public NodeFacet getThisFacet(HikariDataSource ds, int idFacet, String idThesaurus, String lang) { Connection conn;/*from ww w.j a v a 2s . c o m*/ Statement stmt; ResultSet resultSet; NodeFacet nf = new NodeFacet(); try { // Get connection from pool conn = ds.getConnection(); try { stmt = conn.createStatement(); try { String query = "SELECT node_label.lexical_value, thesaurus_array.id_concept_parent FROM node_label, thesaurus_array" + " WHERE node_label.facet_id=thesaurus_array.facet_id" + " and node_label.facet_id ='" + idFacet + "'" + " and node_label.lang = '" + lang + "'" + " and node_label.id_thesaurus = '" + idThesaurus + "'" + " order by node_label.lexical_value DESC"; stmt.executeQuery(query); resultSet = stmt.getResultSet(); resultSet.next(); nf.setIdFacet(idFacet); nf.setIdConceptParent(resultSet.getString("id_concept_parent")); if (resultSet.getRow() == 0) { nf.setLexicalValue(""); } else { nf.setLexicalValue(resultSet.getString("lexical_value")); } } finally { stmt.close(); } } finally { conn.close(); } } catch (SQLException sqle) { // Log exception log.error("Error while getting Facet : " + idFacet, sqle); } return nf; }
From source file:mom.trd.opentheso.bdd.helper.NoteHelper.java
/** * Cette fonction permet de savoir si la Note d'un Concept existe ou non * * @param ds//ww w . j a v a 2s . co m * @param idTerm * @param idThesaurus * @param idLang * @param noteTypeCode * @return boolean */ public boolean isNoteExistOfTerm(HikariDataSource ds, String idTerm, String idThesaurus, String idLang, String noteTypeCode) { Connection conn; Statement stmt; ResultSet resultSet; boolean existe = false; try { // Get connection from pool conn = ds.getConnection(); try { stmt = conn.createStatement(); try { String query = "select id from note" + " where id_term = '" + idTerm + "'" + " and id_thesaurus = '" + idThesaurus + "'" + " and lang ='" + idLang + "'" + " and noteTypeCode = '" + noteTypeCode + "'"; stmt.executeQuery(query); resultSet = stmt.getResultSet(); if (resultSet != null) { resultSet.next(); existe = resultSet.getRow() != 0; } } finally { stmt.close(); } } finally { conn.close(); } } catch (SQLException sqle) { // Log exception log.error("Error while asking if Note of Term exist : " + idTerm, sqle); } return existe; }
From source file:mom.trd.opentheso.bdd.helper.NoteHelper.java
/** * Cette fonction permet de savoir si la Note d'un Concept existe ou non * * @param ds/*from w ww .j a v a2 s.c o m*/ * @param idConcept * @param idThesaurus * @param idLang * @param noteTypeCode * @return boolean */ public boolean isNoteExistOfConcept(HikariDataSource ds, String idConcept, String idThesaurus, String idLang, String noteTypeCode) { Connection conn; Statement stmt; ResultSet resultSet; boolean existe = false; try { // Get connection from pool conn = ds.getConnection(); try { stmt = conn.createStatement(); try { String query = "select id from note" + " where id_concept = '" + idConcept + "'" + " and id_thesaurus = '" + idThesaurus + "'" + " and lang ='" + idLang + "'" + " and noteTypeCode = '" + noteTypeCode + "'"; stmt.executeQuery(query); resultSet = stmt.getResultSet(); if (resultSet != null) { resultSet.next(); existe = resultSet.getRow() != 0; } } finally { stmt.close(); } } finally { conn.close(); } } catch (SQLException sqle) { // Log exception log.error("Error while asking if Note of Concept exist : " + idConcept, sqle); } return existe; }
From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java
/** * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#loadUserAccount(java.lang.String) */// w w w.j a va 2 s . c o m public synchronized List<Object> loadUserAccount(final String userGuid) throws UserManagementException { final String methodName = SQLUserManager.CNAME + "#loadUserAccount(final String guid) throws UserManagementException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", userGuid); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object> userAccount = null; try { sqlConn = SQLUserManager.dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); stmt = sqlConn.prepareCall("{ CALL loadUserAccount(?) }"); stmt.setString(1, userGuid); // common name if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("ResultSet: {}", resultSet); } if (resultSet.next()) { resultSet.last(); int x = resultSet.getRow(); if (DEBUG) { DEBUGGER.debug("x: {}", x); } if ((x == 0) || (x > 1)) { throw new UserManagementException("No user account was located for the provided data."); } resultSet.first(); userAccount = new ArrayList<Object>( Arrays.asList(resultSet.getString(userAttributes.getCommonName()), resultSet.getString(userAttributes.getUserId()), resultSet.getString(securityAttributes.getLockCount()), resultSet.getString(securityAttributes.getLastLogin()), resultSet.getString(securityAttributes.getExpiryDate()), resultSet.getString(userAttributes.getSurname()), resultSet.getString(userAttributes.getGivenName()), resultSet.getString(userAttributes.getDisplayName()), resultSet.getString(userAttributes.getEmailAddr()), resultSet.getString(userAttributes.getTelephoneNumber()), resultSet.getString(userAttributes.getMemberOf()), resultSet.getString(securityAttributes.getIsSuspended()), resultSet.getString(securityAttributes.getOlrSetupReq()), resultSet.getString(securityAttributes.getOlrLocked()))); if (DEBUG) { DEBUGGER.debug("UserAccount: {}", userAccount); } } } else { throw new UserManagementException("No users were located with the provided information"); } } catch (SQLException sqx) { throw new UserManagementException(sqx.getMessage(), sqx); } finally { try { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if (!(sqlConn == null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } catch (SQLException sqx) { throw new UserManagementException(sqx.getMessage(), sqx); } } return userAccount; }
From source file:net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter.java
/** * @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String, * net.mlw.vlh.ValueListInfo)/*from www . j av a 2 s.c om*/ */ public ValueList getValueList(String name, ValueListInfo info) { if (info.getSortingColumn() == null) { info.setPrimarySortColumn(getDefaultSortColumn()); info.setPrimarySortDirection(getDefaultSortDirectionInteger()); } int numberPerPage = info.getPagingNumberPer(); if (numberPerPage == Integer.MAX_VALUE) { numberPerPage = getDefaultNumberPerPage(); info.setPagingNumberPer(numberPerPage); } Connection connection = null; PreparedStatement statement = null; ResultSet result = null; try { boolean doSqlPaging = ((getAdapterType() & DO_PAGE) == 0); connection = connectionCreator.createConnection(); StringBuffer query = (sqlPagingSupport != null) ? sqlPagingSupport.getPagedQuery(sql) : new StringBuffer(sql); statement = statementBuilder.generate(connection, query, info.getFilters(), sqlPagingSupport == null && doSqlPaging); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query.toString()); } if (showSql) { System.out.println("sql: " + query.toString()); } result = getResultSet(statement, info); if (sqlPagingSupport != null) { PreparedStatement countStatement = null; ResultSet countResult = null; try { StringBuffer countQuery = sqlPagingSupport.getCountQuery(sql); countStatement = statementBuilder.generate(connection, countQuery, info.getFilters(), false); if (showSql) { System.out.println("count sql: " + countQuery.toString()); } countResult = countStatement.executeQuery(); if (countResult.next()) { info.setTotalNumberOfEntries(countResult.getInt(1)); } } finally { JdbcUtil.close(countResult, countStatement, null); } } else if (doSqlPaging) { result.last(); int totalRows = result.getRow(); info.setTotalNumberOfEntries(totalRows); if (numberPerPage == 0) { numberPerPage = getDefaultNumberPerPage(); } int pageNumber = info.getPagingPage(); if (pageNumber > 1) { if ((pageNumber - 1) * numberPerPage > totalRows) { pageNumber = ((totalRows - 1) / numberPerPage) + 1; info.setPagingPage(pageNumber); } } if (pageNumber > 1) { result.absolute((pageNumber - 1) * numberPerPage); } else { result.beforeFirst(); } } List list = processResultSet(name, result, (doSqlPaging) ? numberPerPage : Integer.MAX_VALUE, info); if (!doSqlPaging) { info.setTotalNumberOfEntries(list.size()); } return new DefaultListBackedValueList(list, info); } catch (Exception e) { LOGGER.error(e); throw new RuntimeException(e); } finally { connectionCreator.close(result, statement, connection); } }
From source file:mom.trd.opentheso.bdd.helper.GroupHelper.java
/** * Cette fonction permet de savoir si le Group est vide (pas de concepts) * * @param ds/*w w w . j a v a 2s . c om*/ * @param idGroup * @param idThesaurus * @return */ public boolean isEmptyDomain(HikariDataSource ds, String idGroup, String idThesaurus) { Connection conn; Statement stmt; ResultSet resultSet; boolean group = false; try { // Get connection from pool conn = ds.getConnection(); try { stmt = conn.createStatement(); try { String query = "SELECT id_concept FROM concept" + " WHERE id_thesaurus='" + idThesaurus + "'" + " AND id_group='" + idGroup + "'"; stmt.executeQuery(query); resultSet = stmt.getResultSet(); resultSet.next(); group = (resultSet.getRow() == 0); } finally { stmt.close(); } } finally { conn.close(); } } catch (SQLException sqle) { // Log exception log.error("Error while testing if Group have Concept : " + idGroup, sqle); } return group; }
From source file:mom.trd.opentheso.bdd.helper.GroupHelper.java
/** * Cette fonction permet de savoir si l'identifiant est un identifiant de * Groupe ou non// ww w . j av a 2 s .c o m * * @param ds * @param idGroup * @param idThesaurus * @return boolean */ public boolean isIdOfGroup(HikariDataSource ds, String idGroup, String idThesaurus) { Connection conn; Statement stmt; ResultSet resultSet; boolean existe = false; try { // Get connection from pool conn = ds.getConnection(); try { stmt = conn.createStatement(); try { String query = "select idgroup from concept_group where " + " idgroup = '" + idGroup + "'" + " and idthesaurus = '" + idThesaurus + "'"; stmt.executeQuery(query); resultSet = stmt.getResultSet(); resultSet.next(); existe = (resultSet.getRow() != 0); } finally { stmt.close(); } } finally { conn.close(); } } catch (SQLException sqle) { // Log exception log.error("Error while Asking if Is Id of Group : " + idGroup, sqle); } return existe; }