Example usage for java.sql ResultSet next

List of usage examples for java.sql ResultSet next

Introduction

In this page you can find the example usage for java.sql ResultSet next.

Prototype

boolean next() throws SQLException;

Source Link

Document

Moves the cursor forward one row from its current position.

Usage

From source file:eu.earthobservatory.testsuite.utils.Utils.java

public static void createdb() throws Exception {
    String url = "";
    ArrayList<String> databases = new ArrayList<String>();
    PreparedStatement pst = null;

    //Read properties
    Properties properties = new Properties();
    InputStream propertiesStream = Utils.class.getResourceAsStream(dbPropertiesFile);
    properties.load(propertiesStream);//from  w  ww  . j a  v a2  s . com

    if ((databaseTemplateName = System.getProperty("postgis.databaseTemplateName")) == null) {
        databaseTemplateName = properties.getProperty("postgis.databaseTemplateName");
    }

    if ((serverName = System.getProperty("postgis.serverName")) == null) {
        serverName = properties.getProperty("postgis.serverName");
    }

    if ((username = System.getProperty("postgis.username")) == null) {
        username = properties.getProperty("postgis.username");
    }

    if ((password = System.getProperty("postgis.password")) == null) {
        password = properties.getProperty("postgis.password");
    }

    if ((port = System.getProperty("postgis.port")) == null) {
        port = properties.getProperty("postgis.port");
    }

    //Connect to server and create the temp database
    url = "jdbc:postgresql://" + serverName + ":" + port;
    conn = DriverManager.getConnection(url, username, password);

    pst = conn.prepareStatement("SELECT * FROM pg_catalog.pg_database");
    ResultSet rs = pst.executeQuery();

    while (rs.next()) {
        databases.add(rs.getString(1));
    }
    rs.close();
    pst.close();

    databaseName = "teststrabon" + (int) (Math.random() * 10000);
    while (databases.contains(databaseName)) {
        databaseName += "0";
    }

    pst = conn.prepareStatement("CREATE DATABASE " + databaseName + " TEMPLATE " + databaseTemplateName);
    pst.executeUpdate();
    pst.close();
    conn.close();

    url = "jdbc:postgresql://" + serverName + ":" + port + "/" + databaseName;
    conn = DriverManager.getConnection(url, username, password);

    strabon = new Strabon(databaseName, username, password, Integer.parseInt(port), serverName, true);
}

From source file:local.Statistics.MostSellCars.java

/**
 * @param args the command line arguments
 *///from   w  w w  . ja  va  2s .c o m
public static void carrosMasVendidos() {
    Graficas_DAO gr = new Graficas_DAO();
    //obtencion de datos
    ResultSet rs = gr.getgraficaautomas();

    try {
        String nameAuto;
        int quantitySales;
        Object[] fila = new Object[3];
        DefaultPieDataset data = new DefaultPieDataset();
        while (rs.next()) {

            nameAuto = rs.getString(3);
            quantitySales = rs.getInt(1);
            data.setValue(nameAuto, quantitySales);
        }
        JFreeChart chart = ChartFactory.createPieChart("GRAFICAS AUTOS MAS VENDIDOS", data, true, true, false);
        ChartFrame frame = new ChartFrame("Autos mas vendidos", chart);
        frame.pack();
        frame.setVisible(true);

        rs.close();
        gr.close();
    } catch (SQLException ex) {
        Logger.getLogger(MostSellCars.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:ch.newscron.referral.ReferralManager.java

/**
 * Helper function: given a resultSet, processes the data into a list of CustomerShortURL objects
 * @param resultSet a ResultSet returned from a database query
 * @return a List of CustomerShortURL objects
 *///w w  w . ja va2  s. c  o  m
private static List<CustomerShortURL> parseResultSet(ResultSet resultSet) {
    List<CustomerShortURL> shortURLList = new ArrayList<>();
    try {
        while (resultSet.next()) {
            CustomerShortURL newShortURL = new CustomerShortURL(Long.parseLong(resultSet.getString("custId")),
                    resultSet.getString("shortUrl"));
            shortURLList.add(newShortURL);
        }
        return shortURLList;
    } catch (SQLException ex) {
        Logger.getLogger(ReferralManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:com.github.dactiv.common.unit.Fixtures.java

/**
 * ,excludeTables.disable.//from  w  w w.j a v  a2  s.c o m
 * @throws SQLException 
 */
public static void deleteAllTable(DataSource h2DataSource, String... excludeTables) throws SQLException {

    List<String> tableNames = new ArrayList<String>();

    try {
        ResultSet rs = h2DataSource.getConnection().getMetaData().getTables(null, null, null,
                new String[] { "TABLE" });

        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            if (Arrays.binarySearch(excludeTables, tableName) < 0) {
                tableNames.add(tableName);
            }
        }

        deleteTable(h2DataSource, tableNames.toArray(new String[tableNames.size()]));

    } catch (SQLException e) {
        throw e;
    }

}

From source file:massbank.extend.ChemicalFormulaUtils.java

/**
 * CIqXg//from   w ww .  jav  a 2s . c  om
 */
public static List<String[]> getIonMassList() throws IOException {
    List<String[]> massList = new ArrayList();
    try {

        Class.forName("com.mysql.jdbc.Driver");
        String conUrl = "jdbc:mysql://localhost/FORMULA_STRUCTURE_RELATION";
        Connection con = DriverManager.getConnection(conUrl, "bird", "bird2006");
        Statement stmt = con.createStatement();
        String sql = "SELECT FORMULA, MASS FROM ION_MASS order by MASS";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String formula = rs.getString("FORMULA");
            String mass = rs.getString("MASS");
            massList.add(new String[] { formula, mass });
        }
        rs.close();
        stmt.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return massList;
}

From source file:com.bluepandora.therap.donatelife.gcmservice.FindDonator.java

/**
 *
 * @param groupId/*from  w ww. j av a  2 s  .co  m*/
 * @param hospitalId
 * @param mobileNumber
 * @return This function will return the List of GCM ID without Requester 
 * mobileNumber's GCM ID;
 */
public static List findDonatorGCMId(String groupId, String hospitalId, DatabaseService dbService) {
    String query = GetQuery.findBestDonatorQuery(groupId, hospitalId);
    Debug.debugLog("FIND DONATOR: ", query);
    ResultSet result = dbService.getResultSet(query);
    List donatorList = new ArrayList<String>();
    Debug.debugLog("DONATOR GCM ID FINDING");

    try {
        while (result.next()) {
            String mobileNumber = result.getString("mobile_number");
            String gcmId = result.getString("gcm_id");

            if (gcmId.length() > VALID_GCM_SIZE_GREATER) {
                Debug.debugLog("Mobile: ", mobileNumber, "GCM Id: ", gcmId);
                donatorList.add(gcmId);
            }
        }
    } catch (SQLException error) {
        Debug.debugLog("FINDING DONATOR'S GCM SQL EXCEPTION!");
    }
    return donatorList;
}

From source file:com.bluepandora.therap.donatelife.gcmservice.FindDonator.java

/**
 *
 * @param mobileNumber/*w  w w  .jav a  2s . co  m*/
 * @return This function will return the List of GCM ID of this mobile
 * Number
 */
public static List findDonatorGCMId(String mobileNumber, DatabaseService dbService) {
    String query = GetQuery.getGcmIdOfDonatorQuery(mobileNumber);
    Debug.debugLog("FIND DONATOR: ", query);
    ResultSet result = dbService.getResultSet(query);
    List donatorList = new ArrayList<String>();
    Debug.debugLog("REQUESTER GCM ID FINDING");

    try {
        while (result.next()) {
            mobileNumber = result.getString("mobile_number");
            String gcmId = result.getString("gcm_id");

            if (gcmId.length() > VALID_GCM_SIZE_GREATER) {
                Debug.debugLog("Mobile: ", mobileNumber, "GCM Id: ", gcmId);
                donatorList.add(gcmId);
            }
        }
    } catch (SQLException error) {
        Debug.debugLog("FINDING DONATOR'S GCM SQL EXCEPTION!");
    }
    return donatorList;
}

From source file:Main.java

public static String getCLOB(int id) throws Exception {
    Connection conn = null;//  ww  w.j a v  a  2 s .c o  m
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT clobData FROM tableName WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Clob clob = rs.getClob(1);
        // materialize CLOB onto client
        String wholeClob = clob.getSubString(1, (int) clob.length());
        return wholeClob;
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:com.egt.core.db.util.SqlAgent.java

private static SqlAgentMessage executeProcedure(String procedimiento, Long rastro, Object[] args,
        boolean logging) {
    Bitacora.trace(SqlAgent.class, "executeProcedure", procedimiento, rastro);
    Utils.traceObjectArray(args);/*from  w  w w .  j  a v a  2  s .  c  o  m*/
    String procedure = StringUtils.trimToEmpty(procedimiento);
    //      String archivo = logging ? getLogFileName(rastro) : null;
    String archivo = null;
    EnumCondicionEjeFun condicion = EnumCondicionEjeFun.EJECUCION_EN_PROGRESO;
    String mensaje = TLC.getBitacora().info(CBM2.PROCESS_EXECUTION_BEGIN, procedure);
    boolean ok = Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
    if (ok) {
        try {
            // <editor-fold defaultstate="collapsed">
            //              Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            //              if (resultado == null) {
            //                  condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
            //                  mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
            //              } else {
            //                  condicion = EnumCondicionEjeFun.EJECUTADO_SIN_ERRORES;
            //                  if (resultado instanceof Number) {
            //                      mensaje = TLC.getBitacora().info(procedure + " = " + resultado);
            //                  } else if (resultado instanceof ResultSet) {
            //                      ResultSet resultSet = (ResultSet) resultado;
            //                      if (resultSet.next()) {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + resultSet.getObject(1));
            //                      } else {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + STP.STRING_VALOR_NULO);
            //                      }
            //                  } else {
            //                      mensaje = TLC.getBitacora().warn(CBM2.PROCESS_EXECUTION_END, procedure);
            //                  }
            //              }
            // </editor-fold>
            Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            if (resultado == null) {
                condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
                mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
            } else {
                condicion = EnumCondicionEjeFun.EJECUTADO_SIN_ERRORES;
                mensaje = TLC.getBitacora().info(CBM2.PROCESS_EXECUTION_END, procedure);
                if (resultado instanceof ResultSet) {
                    ResultSet resultSet = (ResultSet) resultado;
                    if (resultSet.next()) {
                        mensaje += " (" + resultSet.getObject(1) + ") ";
                    }
                } else if (resultado.getClass().isPrimitive()) {
                    mensaje += " (" + resultado + ") ";
                }
            }
        } catch (Exception ex) {
            condicion = EnumCondicionEjeFun.EJECUTADO_CON_ERRORES;
            mensaje = ThrowableUtils.getString(ex);
            TLC.getBitacora().fatal(ex);
            TLC.getBitacora().fatal(CBM2.PROCESS_EXECUTION_ABEND, procedure);
        } finally {
            Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
            //              DB.close(resultSet);
        }
    } else {
        condicion = EnumCondicionEjeFun.EJECUCION_CANCELADA;
        mensaje = TLC.getBitacora().error(CBM2.PROCESS_EXECUTION_ABEND, procedure);
    }
    SqlAgentMessage message = new SqlAgentMessage(procedure);
    message.setArgs(args);
    message.setRastro(rastro);
    message.setCondicion(condicion);
    message.setArchivo(archivo);
    message.setMensaje(mensaje);
    return message;
}

From source file:ips1ap101.lib.core.db.util.SqlAgent.java

private static SqlAgentMessage executeProcedure(String procedimiento, Long rastro, Object[] args,
        boolean logging) {
    Bitacora.trace(SqlAgent.class, "executeProcedure", procedimiento, rastro);
    Utils.traceObjectArray(args);//from   w  w w . j ava2  s.  c o m
    String procedure = StringUtils.trimToEmpty(procedimiento);
    //      String archivo = logging ? getLogFileName(rastro) : null;
    String archivo = null;
    CondicionEjeFunEnumeration condicion = CondicionEjeFunEnumeration.EJECUCION_EN_PROGRESO;
    String mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_BEGIN, procedure);
    boolean ok = Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
    if (ok) {
        try {
            // <editor-fold defaultstate="collapsed">
            //              Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            //              if (resultado == null) {
            //                  condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
            //                  mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
            //              } else {
            //                  condicion = CondicionEjeFunEnumeration.EJECUTADO_SIN_ERRORES;
            //                  if (resultado instanceof Number) {
            //                      mensaje = TLC.getBitacora().info(procedure + " = " + resultado);
            //                  } else if (resultado instanceof ResultSet) {
            //                      ResultSet resultSet = (ResultSet) resultado;
            //                      if (resultSet.next()) {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + resultSet.getObject(1));
            //                      } else {
            //                          mensaje = TLC.getBitacora().info(procedure + " = " + STP.STRING_VALOR_NULO);
            //                      }
            //                  } else {
            //                      mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_END, procedure);
            //                  }
            //              }
            // </editor-fold>
            Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args);
            if (resultado == null) {
                condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
                mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
            } else {
                condicion = CondicionEjeFunEnumeration.EJECUTADO_SIN_ERRORES;
                mensaje = TLC.getBitacora().info(CBM.PROCESS_EXECUTION_END, procedure);
                if (resultado instanceof ResultSet) {
                    ResultSet resultSet = (ResultSet) resultado;
                    if (resultSet.next()) {
                        mensaje += " (" + resultSet.getObject(1) + ") ";
                    }
                } else if (resultado.getClass().isPrimitive()) {
                    mensaje += " (" + resultado + ") ";
                }
            }
        } catch (Exception ex) {
            condicion = CondicionEjeFunEnumeration.EJECUTADO_CON_ERRORES;
            mensaje = ThrowableUtils.getString(ex);
            TLC.getBitacora().fatal(ex);
            TLC.getBitacora().fatal(CBM.PROCESS_EXECUTION_ABEND, procedure);
        } finally {
            Auditor.grabarRastroProceso(rastro, condicion, archivo, mensaje);
            //              DB.close(resultSet);
        }
    } else {
        condicion = CondicionEjeFunEnumeration.EJECUCION_CANCELADA;
        mensaje = TLC.getBitacora().error(CBM.PROCESS_EXECUTION_ABEND, procedure);
    }
    SqlAgentMessage message = new SqlAgentMessage(procedure);
    message.setArgumentos(args);
    message.setRastro(rastro);
    message.setCondicion(condicion);
    message.setArchivo(archivo);
    message.setMensaje(mensaje);
    return message;
}