Get last sql server information from netbeans jdbc

Asked

Viewed 89 times

-1

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

        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 bring the result in the form of Joptionpane or in the format of Jtextfield for testing.

I used it this way to try to bring the values and yet without success.

int valor = rs.getInt(0);
jTextField6.setText(String.valueOf(valor));
  • Isn’t it exactly the same doubt I answered? From the code it seems you didn’t try as I explained it.

  • Well, there we went in a question and we ended up bringing the matter of time, as I am very doubtful in this part and I researched again and I’m sure other people will also have, I thought valid to bring a topic just for your doubt.

  • I suggested a query for sql server that returns the last line.

  • Yes @Articuno, but with it did not return anything, with this by menso returns something.

  • In fact it even returns, but returns the same thing. 'Sqlserverresultset:3' adding +1 every 5 sgundos.

  • I took the time question and even so it returns the same way, but without adding +1

  • Tried to search a DBMS instead of via programming? If not, do it and compare the results

  • yes, the "Select Max" brings me the value. Is it not because of st or rs?

  • That is the mistake: jTextField6.setText(rs.toString()); realizes that he’s turning the resultset into a string and not picking up what he returned?

  • in this case how can I do? I can’t bring it float or int

  • It depends on the type of this field. The right way is to recover by the getters of the resultset, where it is usually getXXX, and this XXX is the type of the field. If you don’t know which one to use, read the resultset documentation and see the type most suitable for the field you are researching.

  • Ok, in my case I need to bring values like float.

  • If it is float, why ta using getint? You looked at the link I sent?

  • Yes, but I tried with int to see if it would work. I looked yes, but I did not understand very well.

Show 9 more comments

1 answer

2

As I told you in another 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 you said in the comments, the value returned is float, so you use the method getFloat of the 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>)));
  • Articuno, I tried it your way, but it didn’t work. I managed to do it in a way that worked and I believe that will help a lot of people. I would like to post the answer so that people see the solution. how can I do?

  • @Lucas16 what did not work? If you do not detail I can not correct in the answer.

  • Started giving this error "with.microsoft.sqlserver.jdbc.Sqlserverexception: The result set has no current line."

  • @Lucas16 This code does not generate this error. You need to be more specific.

  • I tried several ways to solve his code, but he was not finding the column nor the line. then gave error to pass to float. to solve this problem I did this way. I will post and you strip later for you to understand better

  • @Lucas16 edits the question and shows the schema of your table and how are some data from it

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.