Doubt when creating a SELECT * FROM people from java [closed]

-2
public void mostrar_personas() throws SQLException{
        Connection co = null;
        PreparedStatement preparedStatement = null;



        String consulta = "SELECT * "
                      +   "FROM personas";

        try {


            co = iniciarConnection();
          preparedStatement = (PreparedStatement) co.prepareStatement(consulta);



            System.out.println();
        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (co != null) {
                co.close();
            }

        }

    }

This is the first time I've been handling Database from Java and I have doubts that I would greatly appreciate an answer, again, thank you very much in advance to all

    
asked by Selito95 28.06.2017 в 16:34
source

1 answer

3

Let's see:

In this case you do not necessarily need a prepared statement. Why ?, because the query does not receive data from the outside, as would be the case of a INSERT a UPDATE or a SELECT with a criterion WHERE .

The code below is the official Java example, for this type of query . In the link there are also explanations on how the ResultSets and the rows are read, the only thing is that it is in English, but hey, it is not that complicated to understand.

You'll see that:

Let's say that the Statement serves to pass the query the BD without having to prepare it, because in this case it is not necessary.

  • its uses a ResultSet object to store the obtained data and to cross them, using for example: rs.getString("COF_NAME") this is equivalent to obtain the data of the column of the table called COF_NAME which is, say of the type VARCHAR or any other equivalent to string. The column SALES is of integer type, that's why your data is obtained with rs.getInt("SALES");

            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(); }
                }
            }
    
answered by 28.06.2017 / 16:46
source