Catch latest information sql server by netbeans jdbc

-1

I am using this way to try to bring the latest information registered in the sql server, but for some unknown reason it is not working. I searched in several forums and apparently the same method I'm using is used:

        Class.forName(Auxiliar1.AcessoBanco.getDriver());
        Connection con = DriverManager.getConnection(Auxiliar1.AcessoBanco.getUrl(), Auxiliar1.AcessoBanco.getUser(), Auxiliar1.AcessoBanco.getPass());
        String query1 = "Select MAX(ExtA_IndParafuso) FROM L11";

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query1);
        jTextField6.setText(rs.toString());
        JOptionPane.showMessageDialog(null, rs.getInt(0));

I'm trying to get the result in Joptionpane or in Jtextfield format for testing.

I used it that way to try to bring the values and even then to no avail.

int valor = rs.getInt(0);
jTextField6.setText(String.valueOf(valor));
    
asked by anonymous 01.11.2017 / 12:22

1 answer

2

As I told you in the other question , to return the last row of a table the query is this:

SELECT TOP 1 * FROM <tabela> ORDER BY <campo a ser ordenado> DESC

The query you are using will return the highest value of the searched column, not necessarily this will always be the last, unless that column values are always added in ascending order.

As I said in the comments, the value returned is float, so you use the% method of% Resultset:

Class.forName(Auxiliar1.AcessoBanco.getDriver());
Connection con = DriverManager.getConnection(Auxiliar1.AcessoBanco.getUrl(), Auxiliar1.AcessoBanco.getUser(), Auxiliar1.AcessoBanco.getPass());
String query1 = "SELECT TOP 1 * FROM L11 ORDER BY ExtA_IndParafuso DESC";

Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query1);
jTextField6.setText(String.valueOf(rs.getFloat(<nome da coluna>)));
    
01.11.2017 / 13:10