List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
From source file:com.keybox.manage.db.SystemStatusDB.java
/** * returns the first system that authorized keys has not been tried * * @param userId user id//from w w w. jav a 2 s . co m * @return hostSystem systems for authorized_keys replacement */ public static HostSystem getNextPendingSystem(Long userId) { HostSystem hostSystem = null; Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement( "select * from status where (status_cd like ? or status_cd like ? or status_cd like ?) and user_id=? order by id asc"); stmt.setString(1, HostSystem.INITIAL_STATUS); stmt.setString(2, HostSystem.AUTH_FAIL_STATUS); stmt.setString(3, HostSystem.PUBLIC_KEY_FAIL_STATUS); stmt.setLong(4, userId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { hostSystem = SystemDB.getSystem(con, rs.getLong("id")); hostSystem.setStatusCd(rs.getString(STATUS_CD)); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); return hostSystem; }
From source file:com.keybox.manage.db.SystemStatusDB.java
/** * returns key placement status of system * * @param systemId system id// w w w . ja v a 2s. co m * @param userId user id */ public static HostSystem getSystemStatus(Long systemId, Long userId) { Connection con = null; HostSystem hostSystem = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from status where id=? and user_id=?"); stmt.setLong(1, systemId); stmt.setLong(2, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { hostSystem = SystemDB.getSystem(con, rs.getLong("id")); hostSystem.setStatusCd(rs.getString(STATUS_CD)); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); return hostSystem; }
From source file:common.DBHelper.java
/** * Extract key from given ResultSet./*from www.j a va 2s . com*/ * * @param key resultSet with key * @return key from given result set * @throws SQLException when operation fails */ public static Long getId(ResultSet key) throws SQLException { if (key.getMetaData().getColumnCount() != 1) { throw new IllegalArgumentException("Given ResultSet contains more columns"); } if (key.next()) { Long result = key.getLong(1); if (key.next()) { throw new IllegalArgumentException("Given ResultSet contains more rows"); } return result; } else { throw new IllegalArgumentException("Given ResultSet contain no rows"); } }
From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java
protected static Map getPrimaryKeys(String table_name, DatabaseMetaData dbm) throws SQLException { // returns { table_name(colname,...) : index_name } Map result = new HashMap(5); ResultSet rs = dbm.getPrimaryKeys(null, null, table_name); String prev_index_name = null; String columns = null;//from w ww .j av a 2 s . co m while (rs.next()) { String index_name = rs.getString(6); if (prev_index_name != null && !prev_index_name.equals(index_name)) { result.put(table_name + '(' + columns + ')', prev_index_name); columns = null; } // column_name might be quoted, so unquote it before proceeding String column_name = unquote(rs.getString(4), dbm.getIdentifierQuoteString()); if (columns == null) columns = column_name; else columns = columns + "," + column_name; prev_index_name = index_name; } rs.close(); if (prev_index_name != null) result.put(table_name + '(' + columns + ')', prev_index_name); return result; }
From source file:dsd.dao.DAOProvider.java
/** * the insert row function done in a secure way * /* ww w .ja v a 2s . co m*/ * @param table * @param fields * @param con * @param valueArray * @return * @throws SQLException */ public static int InsertRowSecure(String table, String fields, Connection con, Object[] valueArray) throws SQLException { try { String values = ""; if (valueArray.length > 0) { values = "?"; } for (int i = 1; i < valueArray.length; i++) { values += ",?"; } PreparedStatement command = con .prepareStatement(String.format("insert into %s (%s) values (%s)", table, fields, values)); for (int i = 0; i < valueArray.length; i++) { SetParameter(command, valueArray[i], i + 1); } command.executeUpdate(); command = con.prepareStatement(String.format("select Max(ID) from %s", table)); ResultSet rs = command.executeQuery(); rs.next(); return rs.getInt(1); } catch (Exception ex) { ex.printStackTrace(); } return 0; }
From source file:com.bt.aloha.testing.DbTestCase.java
private static String dump(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int colmax = meta.getColumnCount(); Object o = null;//from w w w . j a v a 2s .c o m StringBuffer sb = new StringBuffer(); for (; rs.next();) { for (int i = 0; i < colmax; i++) { o = rs.getObject(i + 1); if (o != null) sb.append(o.toString()); else sb.append("null"); if (i < colmax - 1) sb.append(" "); } if (!rs.isLast()) sb.append("\n"); } return sb.toString(); }
From source file:dsd.dao.CalculatedDataDAO.java
public static long GetCount(eCalculatedDataType eDataType) { long count = 0; try {// w w w. j a v a 2s .com Connection con = DAOProvider.getDataSource().getConnection(); try { String tableName = GetTableNameForDataType(eDataType); ResultSet results = DAOProvider.SelectTableSecure(tableName, " count(*) ", "", "", con, null); while (results.next()) { count = results.getLong(1); } } catch (Exception exc) { exc.printStackTrace(); } con.close(); } catch (Exception exc) { exc.printStackTrace(); } return count; }
From source file:CreateNewType.java
private static Vector getDataTypes(Connection con, String typeToCreate) throws SQLException { String structName = null, distinctName = null, javaName = null; // create a vector of class DataType initialized with // the SQL code, the SQL type name, and two null entries // for the local type name and the creation parameter(s) Vector dataTypes = new Vector(); dataTypes.add(new DataType(java.sql.Types.BIT, "BIT")); dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT")); dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT")); dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER")); dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT")); dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT")); dataTypes.add(new DataType(java.sql.Types.REAL, "REAL")); dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE")); dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC")); dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL")); dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR")); dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR")); dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR")); dataTypes.add(new DataType(java.sql.Types.DATE, "DATE")); dataTypes.add(new DataType(java.sql.Types.TIME, "TIME")); dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP")); dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY")); dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY")); dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY")); dataTypes.add(new DataType(java.sql.Types.NULL, "NULL")); dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER")); dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB")); dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB")); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { int codeNumber = rs.getInt("DATA_TYPE"); String dbmsName = rs.getString("TYPE_NAME"); String createParams = rs.getString("CREATE_PARAMS"); if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;// w w w . j a va2 s . c o m else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName; else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName; else { for (int i = 0; i < dataTypes.size(); i++) { // find entry that matches the SQL code, // and if local type and params are not already set, // set them DataType type = (DataType) dataTypes.get(i); if (type.getCode() == codeNumber) { type.setLocalTypeAndParams(dbmsName, createParams); } } } } if (typeToCreate.equals("s")) { int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT }; rs = dbmd.getUDTs(null, "%", "%", types); while (rs.next()) { String typeName = null; DataType dataType = null; if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "." + rs.getString(3); else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1); switch (rs.getInt(5)) { case Types.STRUCT: dataType = new DataType(Types.STRUCT, typeName); dataType.setLocalTypeAndParams(structName, null); break; case Types.DISTINCT: dataType = new DataType(Types.DISTINCT, typeName); dataType.setLocalTypeAndParams(distinctName, null); break; case Types.JAVA_OBJECT: dataType = new DataType(Types.JAVA_OBJECT, typeName); dataType.setLocalTypeAndParams(javaName, null); break; } dataTypes.add(dataType); } } return dataTypes; }
From source file:Emporium.Controle.ContrVpne.java
public static ArrayList<Vpne> listaVpne(String where, String nomeBD, int idCli) { String sql = "SELECT * FROM vpne WHERE idCliente = " + idCli + " "; sql = sql + where + " ;"; Connection conn = Conexao.conectar(nomeBD); ArrayList<Vpne> listaVpne = new ArrayList<Vpne>(); try {/*from ww w .ja v a 2 s .c om*/ PreparedStatement valores = conn.prepareStatement(sql); valores.executeQuery(); ResultSet result = (ResultSet) valores.executeQuery(); while (result.next()) { int idCliente = result.getInt("idCliente"); int idDepartamento = result.getInt("idDepartamento"); String nomeDepto = result.getString("nomeDepartamento"); String sro = result.getString("sro"); String descricao = result.getString("descricao"); String valor = result.getString("valor"); String remetente = result.getString("remetente"); String cnpj_remetente = result.getString("cnpj_remetente"); String rlogradouro = result.getString("rlogradouro"); String rnumero = result.getString("rnumero"); String rbairro = result.getString("rbairro"); String rcidade = result.getString("rcidade"); String ruf = result.getString("ruf"); String destinatario = result.getString("destinatario"); String cpf_cnpj_dest = result.getString("cpf_cnpj_dest"); String dlogradouro = result.getString("dlogradouro"); String dnumero = result.getString("dnumero"); String dbairro = result.getString("dbairro"); String dcidade = result.getString("dcidade"); String dcep = result.getString("dcep"); String duf = result.getString("duf"); String data = result.getString("data"); Destinatario remVpne = new Destinatario(remetente, cnpj_remetente, rlogradouro, rnumero, rbairro, rcidade, ruf); Destinatario destVpne = new Destinatario(destinatario, cpf_cnpj_dest, dlogradouro, dnumero, dbairro, dcidade, dcep, duf); Vpne vp = new Vpne(sro, descricao, valor, idCliente, idDepartamento, nomeDepto, data, remVpne, destVpne); listaVpne.add(vp); } valores.close(); return listaVpne; } catch (SQLException e) { Logger.getLogger(ContrVpne.class.getName()).log(Level.WARNING, e.getMessage(), e); return listaVpne; } finally { Conexao.desconectar(conn); } }