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:com.seventh_root.ld33.common.player.Player.java

public static Player getByUUID(Connection databaseConnection, UUID uuid) throws SQLException {
    if (playersByUUID.containsKey(uuid.toString()))
        return playersByUUID.get(uuid.toString());
    if (databaseConnection != null) {
        PreparedStatement statement = databaseConnection.prepareStatement(
                "SELECT uuid, name, password_hash, password_salt, resources FROM player WHERE uuid = ? LIMIT 1");
        statement.setString(1, uuid.toString());
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            Player player = new Player(databaseConnection, UUID.fromString(resultSet.getString("uuid")),
                    resultSet.getString("name"), resultSet.getString("password_hash"),
                    resultSet.getString("password_salt"), resultSet.getInt("resources"));
            cachePlayer(player);/*from   w  w  w. ja  va2 s . com*/
            return player;
        }
    }
    return null;
}

From source file:edu.jhu.pha.vospace.jobs.JobsProcessor.java

/**
 * Returns the JobDescription object serialized from the database record
 * @param jobId The identifier of a job//from   ww  w .j  ava2 s .c  om
 * @return The job java object
 */
public static JobDescription getJob(final UUID jobId) {
    return DbPoolServlet.goSql("GetJob request", "select json_notation, note from jobs where id = ?",
            new SqlWorker<JobDescription>() {
                @Override
                public JobDescription go(Connection conn, PreparedStatement stmt) throws SQLException {
                    JobDescription returnJob = null;
                    stmt.setString(1, jobId.toString());
                    ResultSet rs = stmt.executeQuery();
                    if (rs.next()) {
                        byte[] jobJsonNotation = rs.getBytes(1);
                        try {
                            returnJob = (new ObjectMapper()).readValue(jobJsonNotation, 0,
                                    jobJsonNotation.length, JobDescription.class);
                            returnJob.setNote(rs.getString("note"));
                        } catch (JsonMappingException ex) { // Shouldn't happen
                            throw new InternalServerErrorException(ex.getMessage());
                        } catch (JsonParseException ex) {
                            throw new InternalServerErrorException(ex.getMessage());
                        } catch (IOException ex) {
                            throw new InternalServerErrorException(ex.getMessage());
                        }
                    }
                    return returnJob;
                }
            });
}

From source file:Emporium.Controle.ContrDestinatarioImporta.java

public static int inserir(int idCliente, int idDepartamento, String nome, String cpf_cnpj, String empresa,
        String cep, String endereco, String numero, String complemento, String bairro, String cidade, String uf,
        String email, String celular, String pais, String nomeBD, String tags) {
    Connection conn = Conexao.conectar(nomeBD);
    String sql = "INSERT INTO cliente_destinatario (idCliente, nome, cpf_cnpj, empresa, cep, endereco, numero, complemento, bairro, cidade, uf, email, celular, pais, tags, idDepartamento) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    //System.out.println("inserir Destinatario -----------------\n"+sql+"\n---------------");

    try {// w w  w .  j av  a 2  s .  c om
        PreparedStatement valores = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        valores.setInt(1, idCliente);
        valores.setString(2, FormataString.removeSpecialChars(nome));
        valores.setString(3, cpf_cnpj);
        valores.setString(4, empresa);
        valores.setString(5, cep);
        valores.setString(6, FormataString.removeSpecialChars(endereco));
        valores.setString(7, numero);
        valores.setString(8, complemento);
        valores.setString(9, bairro);
        valores.setString(10, cidade);
        valores.setString(11, uf);
        valores.setString(12, email);
        valores.setString(13, celular);
        valores.setString(14, pais);
        valores.setString(15, tags);
        valores.setInt(16, idDepartamento);
        valores.executeUpdate();
        int autoIncrementKey = 0;
        ResultSet rs = valores.getGeneratedKeys();
        if (rs.next()) {
            autoIncrementKey = rs.getInt(1);
        }
        valores.close();
        return autoIncrementKey;
    } catch (SQLException e) {
        //System.out.println("ERRO > "+e);
        ContrErroLog.inserir("HOITO - ContrPreVendaDest.inserir", "SQLException", sql, e.toString());
        return 0;
    } finally {
        Conexao.desconectar(conn);
    }
}

From source file:Main.java

/**
 * /*from  ww  w.j  ava 2  s. c om*/
 * @param conn
 * @param table
 * @param c_name
 */
public static void updateContentVersion(Connection conn, String table, String c_name) throws Exception {
    ResultSet rs;
    Statement s;
    int v;

    s = conn.createStatement();
    rs = s.executeQuery("SELECT c_version FROM " + table + " WHERE c_name = '" + c_name + "'");
    v = 1;

    if (rs.next()) {
        v = rs.getInt(1);
        v++;
    }

    rs.close();

    s.executeUpdate("UPDATE " + table + " SET c_version = " + v + " WHERE c_name = '" + c_name + "'");
    s.close();
}

From source file:net.solarnetwork.node.dao.jdbc.derby.DerbyCustomFunctionsInitializer.java

private static void registerBitwiseFunctions(final Connection con, String schema) throws SQLException {
    DatabaseMetaData dbMeta = con.getMetaData();
    ResultSet rs = dbMeta.getFunctions(null, null, null);
    Set<String> functionNames = new HashSet<String>(Arrays.asList(BITWISE_AND, BITWISE_OR));
    while (rs.next()) {
        String schemaName = rs.getString(2);
        String functionName = rs.getString(3).toUpperCase();
        if (schema.equalsIgnoreCase(schemaName) && functionNames.contains(functionName)) {
            functionNames.remove(functionName);
        }//  w  w w. jav  a  2 s  . co m
    }

    // at this point, functionNames contains the functions we need to create
    if (functionNames.size() > 0) {
        final String sqlTemplate = "CREATE FUNCTION %s.%s( parm1 INTEGER, param2 INTEGER ) "
                + "RETURNS INTEGER LANGUAGE JAVA DETERMINISTIC PARAMETER STYLE JAVA NO SQL "
                + "EXTERNAL NAME 'net.solarnetwork.node.dao.jdbc.derby.ext.DerbyBitwiseFunctions.%s'";
        if (functionNames.contains(BITWISE_AND)) {
            final String sql = String.format(sqlTemplate, schema, BITWISE_AND, "bitwiseAnd");
            con.createStatement().execute(sql);
        }
        if (functionNames.contains(BITWISE_OR)) {
            final String sql = String.format(sqlTemplate, schema, BITWISE_OR, "bitwiseOr");
            con.createStatement().execute(sql);
        }
    }
}

From source file:Main.java

public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {/*from  w w w.  j av a  2s .  co m*/
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Blob blob = rs.getBlob(3);
        // materialize BLOB onto client
        return blob.getBytes(1, (int) blob.length());
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java

public static void relatoriosVendas() throws SQLException, ClassNotFoundException {
    String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM FILIAL FL "
            + "INNER JOIN FUNCIONARIO FUNC ON FUNC.ID_FILIAL = FL.ID_FILIAL "
            + "INNER JOIN ALUGUEL AL ON AL.ID_FUNCIONARIO = FUNC.ID_FUNCIONARIO " + "GROUP BY FL.NOME_FILIAL";
    Connection conn = Conexoes.obterConexao();
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery(query);

    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    while (rs.next()) {
        ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL"));
    }/*from   w w  w . jav a  2  s .  com*/

    JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Aluguis", "Filiais",
            "Separadas por filiais", ds, PlotOrientation.VERTICAL, true, true, false);

    try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\vendas.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) {
    try (OutputStream arquivo = new FileOutputStream(
            "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }
}

From source file:Main.java

public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {/*w  ww . java2 s.co  m*/
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Blob blob = rs.getBlob("photo");
        // materialize BLOB onto client
        return blob.getBytes(1, (int) blob.length());
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:com.freemedforms.openreact.db.DbSchema.java

/**
 * Determine if a patch has been applied yet.
 * // w w w .  j a v  a2  s  .co m
 * @param patchName
 * @return Success.
 */
public static boolean isPatchApplied(String patchName) {
    Connection c = Configuration.getConnection();

    int found = 0;

    PreparedStatement cStmt = null;
    try {
        cStmt = c.prepareStatement("SELECT COUNT(*) FROM tPatch " + " WHERE patchName = ? " + ";");
        cStmt.setString(1, patchName);

        boolean hadResults = cStmt.execute();
        if (hadResults) {
            ResultSet rs = cStmt.getResultSet();
            rs.next();
            found = rs.getInt(1);
            rs.close();
        }
    } catch (NullPointerException npe) {
        log.error("Caught NullPointerException", npe);
    } catch (Throwable e) {
    } finally {
        DbUtil.closeSafely(cStmt);
        DbUtil.closeSafely(c);
    }

    return (boolean) (found > 0);
}

From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java

public static void relatoriosDisponibilidade() throws SQLException, ClassNotFoundException {
    String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM CARRO CA "
            + "INNER JOIN FILIAL FL ON FL.ID_FILIAL = CA.ID_FILIAL " + "WHERE CA.DISPONIBILIDADE_CARRO = '1' "
            + "GROUP BY FL.NOME_FILIAL";
    Connection conn = Conexoes.obterConexao();
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery(query);

    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    while (rs.next()) {
        ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL"));
    }// w  w  w .  ja v  a2s .  c o  m

    /*File fg = new File("C:\\Users\\bruno.lopes.KRONMED\\Documents\\NetBeansProjects\\LoCarSys\\src\\main\\webapp\\ImagensLoCarSys\\disponibilidade.png");
     fg.delete();*/
    JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Disponibilidade", "Filiais",
            "Separados por filiais", ds, PlotOrientation.VERTICAL, true, true, false);

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
    try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\disponibilidade.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
    try (OutputStream arquivo = new FileOutputStream(
            "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }
}