I use Java, JDBC, PreparedStatement. It is necessary to select from the database 1 entry. But only when it appears, the moment the record appears in the database from 1s to 30. That is, I need a cycle. I understand how it algorithmically looks. But due to poor knowledge of Java syntax, JDBC cannot organize this loop. I see it this way, I arrange the connection, I write the select, if the request returns zero, then I continue to select, until the value returns, after the value of the assert I can check it, of course. Solve the loop either through true / false or 1 - 0. Request to experts to help in solving.

import java.sql.*; class Test1 { public static void main (String[] args) { try { String url = "jdbc:msql://10.10.10.10:1114"; Connection conn = DriverManager.getConnection(url,"",""); Statement stmt = conn.createStatement(); ResultSet rs; rs = stmt.executeQuery("SELECT ID FROM TABLE"); while ( rs.next() ) { String numID = rs.getString("ID"); System.out.println(numID); } conn.close(); } catch (Exception e) { System.err.println(e.getMessage()); } } } 
  • Add the code of your solution - Mikhail Vaysman
  • My apologies, I will send from work tomorrow. - RomanQA
  • better re-create the question tomorrow. since this is hardly anyone tomorrow will be watching. - Mikhail Vaysman
  • Everything is standard, something like this will be if trimmed and without variables in the query. - RomanQA
  • so check returned the request something and if not returned, then wait and do it again - Mikhail Vaysman

1 answer 1

as @Mikhail Vaysman advised, you need to check for the presence of a result, and as soon as they appear, execute your code. This can be done in different ways, for example, to get the count of the table and check that it is not 0. But I would recommend you another way:

Since you are working with a newly returned ResultSet, the cursor of which indicates before the first line, it is easiest to check for data simply by calling isBeforeFirst() . This avoids the need to go back if the data is to be read.

from the documentation:

Returns: true if the cursor is the first row; if the cursor is set

As explained in the documentation, it returns false if the cursor is not before the first entry or if there are no lines in the ResultSet. So the code is correct

 if (!rs.isBeforeFirst() ) { System.out.println("Нет данных"); } 
  • With this solution, I get false and the loop ends. if (! rs.isBeforeFirst ()) {do {String numID = rs.getString (“ID”); System.out.println (numID); } while (rs.next ()); } - RomanQA
  • so it should be in the While loop to add and wait for the data - Senior Pomidor