Work with the results obtained from a query to BBDD from java

1

Good morning,

I can not find the way to work with the results returned by a query to Database from java.

The code where I made the query is the following:

boolean isInsert;
try (PreparedStatement ps = con_actu.prepareStatement("SELECT * FROM info_XboxOne WHERE Codigo_juego = ?")) {
     ps.setString(1, code);
     try (ResultSet rs = ps.executeQuery()) {
           isInsert = !rs.next();
     }
}

How can I enter the data of the results you give me? I need to compare if the values in the database are the same as the ones just removed, to know if I have to update it and have a record of the last time the record was updated.

    
asked by JetLagFox 07.05.2017 в 21:45
source

2 answers

4

I have modified your code so that it approaches what you want to do, I leave it below:

boolean isInsert;
try (PreparedStatement ps = con_actu.prepareStatement("SELECT * FROM info_XboxOne WHERE Codigo_juego = ?")) 
{
     ps.setString(1, code);
     try (ResultSet rs = ps.executeQuery())
      { 
            //debes iterar sobre el ResultSet obtenido de la consulta, si el metodo next() retorna true significa que hay datos en la fila siguiente
            while(rs.next())
            {
                //el metodo getXX puede recibir el nombre de la columna o el indice de la columna (estas comienzan a partir del numero 1 )  
                System.out.println(rs.getString(1));    
            }

           //isInsert = !rs.next();
     }
}

I hope it's helpful

Greetings

    
answered by 07.05.2017 / 23:00
source
1

I'll leave you the code of official example that uses Java :

public static void viewTable(Connection con, String dbName)
    throws SQLException {

    Statement stmt = null;
    String query =
        "select COF_NAME, SUP_ID, PRICE, " +
        "SALES, TOTAL " +
        "from " + dbName + ".COFFEES";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + "\t" + supplierID +
                               "\t" + price + "\t" + sales +
                               "\t" + total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}
  • When the query is executed, the result is stored here: ResultSet rs = stmt.executeQuery(query);
  • You start to scroll through the result set: while (rs.next()) {
  • You get the values using the appropriate method and the name of the column. If it is a String and the column is called COF_NAME : String coffeeName = rs.getString("COF_NAME");
  • The rest speaks for itself
answered by 07.05.2017 в 21:54