Please tell me how to randomly select a row from the database? Here is what I did

ResultSet r1 = (ResultSet) s1.executeQuery(select str from slovar where length(str) > 12); ResultSet r2 = (ResultSet) s1.executeQuery(select count( * ) from slovar); int kol = 0; while (r1.next()) { kol = r2.next(); } int randomStr = (int) Math.random() * kol + 1; 

And how can I choose from line r1 with the number randomStr?

  • @ qwas13, Edit your code. Now he does not read. - Nicolas Chabanovsky
  • where id = randomStr - Gorets
  • I did not prescribe id in the table - qwas13
  • Eh is young-green: come and don't know what is in the SQL table there is no concept of line number ... - Barmaley

2 answers 2

You can try to use this query:

 select str from (select str, rownum as order_number from slovar where length(str) > 12)) where order_number = randomStr 

    Probably the easiest way to know the number of lines in the result of

     r2 = s1.executeQuery("select count( * ) from slovar where length(str) > 12"); 

    almost like yours and calculate the number of a random string (randomStr), as you do, and then limit the size of the output result

    ResultSet r1 = (ResultSet) s1.executeQuery ("select str from slovar where length (str)> 12"); s1.setMaxRows (randomStr);

    then read and at the end you get the desired string

     String str; while (r1.next) str = r1.getString("str"); r1.close();