Random record

I'am looking for a way to randomly pick a record from an Access Database. I found this topic: http://forum.java.sun.com/thread.jspa?forumID=48&threadID=235843, but it isn't really what I am searching.
The problem is that my primary key field isn't successive because of deleting and adding records... So using for example Random = 1 + random.nextInt(10000); to select a primary key will not work because there are numbers missing. Is there another way to randomly select a record?
This is the code I use to connect with the database:
package database;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import domein.Vraag;
public class VraagManager {
     private final static String LEES_VRAAG_SQL = "SELECT * FROM vragen";
     Vraag vraagObject;
     public Vraag getVraag()
          Connection connection = Connectie.getInstance().getConnection();
//           create Statement for querying database
          Statement statement;
          try {
               statement = connection.createStatement();
               // query database
               ResultSet resultSet = statement.executeQuery(LEES_VRAAG_SQL);
                    resultSet.next();
                    String vraag = resultSet.getString("vraag");
                    String antwoord1 = resultSet.getString("antwoord1");
                    String antwoord2 = resultSet.getString("antwoord2");
                    String antwoord3 = resultSet.getString("antwoord3");
                    int correctAntwoord = resultSet.getInt("correctAntwoord");
                    Vraag vraagObject = new Vraag(vraag, antwoord1, antwoord2, antwoord3, correctAntwoord);
               statement.close();
               return vraagObject;
          } catch (SQLException e) {
               e.printStackTrace();
          return null;
}

You are very helpful 8-) Anyway, I found a solution myself...
Maybe I can help other people with this:
Random randomNumber = new Random();
     int randomInt = 1 + randomNumber.nextInt(numberOfRecords);
     for(int i=0; i<randomInt; i++)     
              resultSet.next();

Similar Messages

  • How can I select the random records in u201Cstep loopu201D.

    Hi, Experts,
    I am using step loop to display the data on screen now I want to select the random records from this step loop display. Like 1st 3rd or 5th record, is there any way to select the records,
    like I did in Table control there was a filed in internal table named marked char length 1, I gave this field name in u201Cw/ SelColumnu201D it fill that field with u2018Xu2019 from where I can get the info about the selected records, in this way I can easily perform the operation on that internal table with the help of that marked field.
    Is there any way to select the records in step loop too ?
    Kind Regards,
    Faisal

    thanks for replay shwetali,
    but i just gave you example of random records with 1st 3rd and 5th my goal is not select only these records. i want to select the random mean any records from the step loop display.
    like we can select from the table control. and when select any record it place 'X' in the given internal table field.
    Thanks and kind Regards,
    Faisal

  • Select Random Records with Group By?

    Hi,
    I currently select random records by using this type of SQL:
    (SELECT *
    FROM ( SELECT *
    FROM ( SELECT *
    FROM TABLEA
    ORDER BY dbms_random.value
    WHERE ROWNUM <= 100
    Based on the following data, I now want to still pick out random records but I only want 1 record from the same ID and START_DATE but where the FLAG could be Y or N.
    So with grouping by ID and START_DATE, I need to still be able to choose the record if it has anything from 1 entry or 10 entries per ID and START_DATE.
    Could this be performed with a GROUP BY?
    ID START_DATE FLAG
    1 12/12/2005 Y
    1 12/12/2005 N
    1 12/12/2005 N
    1 30/12/2005 Y
    1 30/12/2005 N
    1 30/12/2005 N
    2 10/10/2005 Y
    2 07/07/2005 Y
    Thanks for any help.
    Daniel

    Try this [not tested]:
    SELECT *
         FROM
              (SELECT *
                   FROM
                   ( SELECT
                              ID
                             ,START_DATE
                             ,FLAG
                             ,ROW_NUMBER() OVER
                                  (PARTITION BY ID,START_DATE ORDER BY dbms_random.value) RN
                        FROM TABLEA
                   ORDER BY dbms_random.value
         WHERE RN = 1
              AND ROWNUM <= 100
         ;

  • Dynamic Table with Random Records

    What I am trying to do is select random records from a table
    and display them in a dynamic table with max columns set to 3 and
    the 4th record to be on a new row. Below is what I have right now
    and it works to randomly pick records but has no function to set
    columns in a table. If there is an easier way feel free to let me
    know. I have tried various ways to do this but none seem to work.
    <CFQUERY NAME="getItems" DATASOURCE="absi">
    SELECT catfit.*, modcats.*, prodmat.*, prod.* FROM catfit,
    modcats,
    prodmat, prod WHERE prodmat.prodid=catfit.prodid And
    catfit.catid=modcats.catid
    ORDER BY modl ASC </cfquery>
    <cfif getItems.recordCount>
    <cfset showNum = 3>
    <cfif showNum gt getItems.recordCount>
    <cfset showNum = getItems.recordCount>
    </cfif>
    <cfset itemList = "">
    <cfloop from="1" to="#getItems.recordCount#"
    index="i">
    <cfset itemList = ListAppend(itemList, i)>
    </cfloop>
    <cfset randomItems = "">
    <cfset itemCount = ListLen(itemList)>
    <cfloop from="1" to="#itemCount#" index="i">
    <cfset random = ListGetAt(itemList, RandRange(1,
    itemCount))>
    <cfset randomItems = ListAppend(randomItems, random)>
    <cfset itemList = ListDeleteAt(itemList,
    ListFind(itemList, random))>
    <cfset itemCount = ListLen(itemList)>
    </cfloop>
    <cfloop from="1" to="#showNum#" index="i">
    <cfoutput>
    <table width="205" border="0" align="left"
    cellpadding="0" cellspacing="0">
    <tr>
    <td width="235" height="116"> <div
    align="center"><img
    src="../Products/ProductPictures/#getitems.pic[ListGetAt(randomItems,
    i)]#" width="100"></div></td>
    </tr>
    <tr>
    <td
    class="ProdTitle">#getitems.brand[ListGetAt(randomItems,
    i)]# #getitems.modl[ListGetAt(randomItems, i)]#</td>
    </tr>
    <tr>
    <td
    class="paragraph">$#getitems.prc[ListGetAt(randomItems,
    i)]#</td>
    </tr>
    <tr>
    <td><A
    href="../Products/details.cfm?prodid=#getItems.prodid[ListGetAt(randomItems,
    i)]#" class="linkcontact">more
    info</a></td>
    </tr>
    <tr>
    <td> </td>
    </tr>
    </table>
    </cfoutput>
    </cfloop>
    </cfif>

    To start a new row after 3 records, do something like this.
    <table>
    <tr>
    <cfoutput query="something">
    <td>#data#<td>
    <cfif currentrow mod 3 is 0>
    </tr><tr>
    </cfoutput>
    </tr>
    </table>
    You should also know that your approach is very inefficient
    in that you are bringing in to cold fusion more data than you need.
    First of all you are selecting every field from 3 tables when you
    don't appear to be using all of them. Second, you are selecting
    every record and you only want to use 3. There are better ways out
    there, but they are db specific and you did not say what you are
    using.

  • Display Random Record

    Morning all
    Just a quick one.
    How do I display a random record in DW?
    I have a MySQL statement which is:
    SELECT * FROM
    tablename ORDER BY RAND() LIMIT 0,
    number of records to display
    The error message I get is:
    'Syntax error (missing operator) in query expression RAND()
    LIMIT 0'
    If I alter 'RAND()' to 'RND()', I still get the error. If I
    remove the '0' I still get the error.
    Any ideas how to make this work?
    I'm using Access BTW.
    Regards
    Martin

    Tell your SQL statement what to RAND - right now it doesn't
    know how to
    randomize it, by what field.
    "Pantyboy" <[email protected]> wrote in
    message
    news:ef0and$r7g$[email protected]..
    > Morning all
    >
    > Just a quick one.
    >
    > How do I display a random record in DW?
    >
    > I have a MySQL statement which is:
    >
    > SELECT * FROM
    tablename ORDER BY RAND() LIMIT 0,
    number of
    > records to
    > display
    >
    > The error message I get is:
    >
    > 'Syntax error (missing operator) in query expression
    RAND() LIMIT 0'
    >
    > If I alter 'RAND()' to 'RND()', I still get the error.
    If I remove the '0'
    > I
    > still get the error.
    >
    > Any ideas how to make this work?
    >
    > I'm using Access BTW.
    >
    > Regards
    >
    > Martin
    >

  • Random Record Selection

    I have a page that I have a recordset on just calling the
    RecordID in the database. How would I have the page or recordset
    select a random record and put that recordID in say a session
    variable?
    Thanks

    MySQL:
    SELECT yourfield FROM yourtable WHERE ... ORDER BY rand()
    LIMIT 1
    MSSQL:
    SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
    newid()
    MS Access:
    SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
    RND(yourprimarykey) [warning: this may not be random through
    CF]
    HTH
    Tim Carley
    www.recfusion.com
    [email protected]

  • Random record return

    I have a DB of sites that I need to query and pull in 5
    records that are to be displayed in a random order.
    I can limit the records but I'm struggling with displaying
    them in a random order.
    Any ideas?

    We're using MySQL as the DB and to clarify I need to pull in
    5 random records in a random order.
    I've just been emailed the answer by a friend so I'llpost it
    up here.
    "SELECT *
    FROM tablename
    WHERE somefield='something'
    ORDER BY RAND() LIMIT 5"
    Seems to work fine to me.

  • Random record for ASP

    I would like to have a section on my ASP page to generate a
    random record that pulls from my database. So that each time the
    page is opened/refreshed, a different record is displayed.
    Any ideas?

    "Biggest Dave" <[email protected]> wrote in
    message
    news:e2nn6n$83e$[email protected]..
    >I would like to have a section on my ASP page to generate
    a random record
    >that pulls from my database. So that each time the page
    is
    >opened/refreshed, a different record is displayed.
    >
    > Any ideas?
    Yes.
    Of course, since I don't know what database you're using, any
    or all of
    those ideas could be useless.

  • Random Record + Update

    Looking to create a random record repeat region where the
    random records being pulled updates a hit count. i.e. a simple
    banner rotator that counts how many times it's displayed. not all
    banners being rotated are in one folder... they are simply accounts
    designated as being featured. I tried the order by rand() limit
    5... error. changed to just rand() and that worked... but how do I
    update a file based on that?

    darrel wrote:
    >> i tried the code you provided, the 2 question
    marks... but that didn't seem
    >> to do it either. anyone else have any ideas on how
    to update a hit counter
    >> based on the random record recordset?
    >
    > Instead of being a smart-***, how about actually either
    a) quoting the
    > original question or b) repeating the question.
    >
    > Anyone that's going to be of any help to you uses the
    newsgroup interface,
    > not the crappy web forum, so when you reply to a
    question that's several
    > weeks/months old, we don't see the original post.
    >
    > So, as such, all we saw was your silly question mark.
    I use a newsreader and saw the entire thread as if it was a
    new one.
    There was 3 days between the original posts and his question
    mark. Maybe
    a question mark wasn't the right way to get more attention to
    the post...
    Steve

  • Display random records

    Hi guys,
    Is there an easy way to display a random record from an XML
    file? Current code:
    HEAD
    <script type="text/javascript">
    <!--
    var dsPower = new Spry.Data.XMLDataSet("broadband.xml",
    "software/product");
    //-->
    </script>
    BODY
    <div spry:region="dsPower">{product_name}</div>
    What i want to happen is when the page is loaded (and/or
    reloaded) i want the {product_name} to randomly display any one of
    my ten product names. I presume it requires use of {ds_RowID} and
    some JavaScript, but am unsure of where to begin.
    Thanks in advance for any help.

    nevermind people, i figured it out already. please close this topic.

  • Random Record Display

    Can someone tell me how to display a random record from a database? I want to use it on my site to show a "review"... am able to do it in ASP but not sure how to in JSP.
    Any advice is gratefully received.
    M

    ok man
    you can put all object that you get it form DB in not
    sorted contener as HashMAP. okYeah, then to further randomize it, you can use a Random Number to lookup in that HashMap. ;) Sounds interesting.
    Annie.

  • Selecting random records out of a table

    Hi,
    I guess this question isn't new, but I didn't found anything about this using the search of this forum.
    How can I get e.g. 5 random records out of a table?
    I have a small table, which stores the special offers of a shop. Let us say, there are 30 records in it. How can I fetch (e.g.) 5 random records of this table?
    I tried to use this statement in a loop:
    SELECT t_item_id FROM tbl_special_offers SAMPLE (1) WHERE ROWNUM = 1
    But this is a very small table, so most of the query returns no data. Additionally I have to check if this number was already selected and ignore it in this case. This isn't a good solution, is it?
    Can somebody help me out with a better solution? Thanks :)

    create or replace package shop_stuff
    is
       function random_number return number;
    end;
    create or replace package body shop_stuff
    is
       function random_number return number
       is
       begin
          return dbms_random.value;
       end;
    begin
       dbms_random.initialize (to_number(to_char(sysdate,'mmddsssss')));
    end;
    create table tbl_special_offers (t_item_id number);
    insert into tbl_special_offers values (1);
    insert into tbl_special_offers values (2);
    insert into tbl_special_offers values (3);
    insert into tbl_special_offers values (4);
    insert into tbl_special_offers values (5);
    insert into tbl_special_offers values (6);
    insert into tbl_special_offers values (7);
    insert into tbl_special_offers values (8);
    insert into tbl_special_offers values (9);
    insert into tbl_special_offers values (10);
    select t_item_id
      from (select t_item_id
              from tbl_special_offers
             order by shop_stuff.random_number)
    where rownum <= 5                       
    SQL> /
    T_ITEM_ID
             6
             7
             5
             4
            10
    SQL> /
    T_ITEM_ID
             1
             5
             3
             9
             6
    SQL> /
    T_ITEM_ID
             6
             5
             3
             2
            10
    SQL> /
    T_ITEM_ID
            10
             2
             8
             4
             1
    SQL>

  • Help with selecting 10 random records from all records meeting report selection criteria in Crystal 11

    <p>I am trying to select ten random records from all that match the report selection criteria then report on each of these random records for QA/QI.  I have tried the RND function however it is giving me a random number rather than a random record selection.  I cannot figure this out and am despirately seeking assistance.</p><p>Thank you,</p><p>Amy</p>

    <p>I don&#39;t know of any Random record selection functions but maybe you could write your own custom function inside of a record selection to randomly filter the records.  You would use the Rand function we currently have to decide if a record was included in the report data or not.</p><p>Another possible option is to filter the records before they get to the report.  You can only do this if you are pushing the data into the report instead of having the report pull the data.  An example of this would be passing an ado recordset to a report at runtime. </p><p>Rob Horne</p><p>http://diamond.businessobjects.com/blog/10 </p>

  • Random Record from a Query

    I have a page on my site that displays certain information in
    kind of a "feature" section, where it needs to query the database
    based on certain criteria and then display the data for just one of
    the records. The record should be different each time the page
    loads. So, if my query has 10 or 20 records, I just want to select
    one of them randomly and disply its data. What would be the best
    way to code such a scenario?
    Many thanks.

    first, a statement that contains "The record
    should be different each time the page loads" and "select one
    of them
    randomly and display its data" is a bit of a contradiction:
    you either
    display a random record or you control which record to
    display to make
    sure each page load displays a different one.
    if you just want a random record displayed (which means same
    record may
    be displayed on subsequent page loads) then how to do it
    depends on
    which database you are using, as cfSearching pointed out.
    here's a great place to look to sql statements for this for
    various db
    systems:
    http://www.petefreitag.com/item/466.cfm
    if you want to display a different record each page load, you
    need to
    keep track which records have already been displayed
    previously and
    display the next one in sequence. you could do this by
    storing the list
    of record ids in an application-scope variable, displaying
    the record
    with the first id and removing that id from the list of ids.
    when the
    list is empty, query the db for record ids again and make a
    list anew.
    hth
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/

  • Random Records are being skipped while uploading data in PSA from CSV File

    Hi Experts,
    I am facing issue in data uploading in PSA through CSV file, Random Records are being skipped while uploading data in PSA.
    First Load
    We have flat file (.txt in CSV format), which contains 380240 Records.
    We are uploading the flat file data into PSA from Application Server. But it uploads Only 380235 records, 5 Records are being skipped.
    Second Load
    We have re-generated same file next day, which contains same No of Records (380240), but this time it uploads Only 380233 records, 7 Records are being skipped.
    We found 1 skipped record (based on key columns combination, by cross verifying from source and PSA table) from First load. But same records (combination of key column) is available in second load. It means same records are not being skipped every time.
    Earlier (5 months ago) , We have loaded 641190 Records from flat file in same PSA and all records (641190) were uploaded successfully.
    There is no change is Source, PSA and flat file structure.
    Thanks & Regards
    Bijendra

    Hi Bijendra,
        Please check in the file if at the begining if it has got any excape sign then that record may be skipped so the records may be mssing
    Please check the excape sign like ; if they are present at the beginign that recor entirely will be skipped.
    Regards
    vamsi

  • Selecting Non repeatable random records

    HI all,
     I have 20 records in table. I want to get a random single  record from that table.And the main thing is
    if i having 20 records 1st time i'm getting a random record.the upcoming execution result that does not show previous random record .
    That is out of 20 records ,each time i want to select record that does not occur previously.so at 20 executions i have 20 result with non-repeatable.
    If i am confusing you,sorry for this.....pls help me to get a suffled record at each time
    note: i'm used NEWID() that gives random records but it occurs previously some time

    I cannot reproduce this :-)
    with
    [10] as (select 0 as c union all select 0),
    [11] as (select 0 as c from [10] as a, [10] as b),
    [12] as (select 0 as c from [11] as a, [11] as b),
    [13] as (select 0 as c from [12] as a, [12] as b),
    nums as (select top(20) row_number() over(order by c) as n from [13]),
    choice as (select top(1) n from nums order by checksum(newid()))
    select * from choice order by n 
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

Maybe you are looking for

  • I am having problem dialing from my iphoe 5 but I can receive calls and 3G is working

    I have an Iphone   IOS 8.2 , occasionly like every couple of weeks I am not able to dial from my phone , however I ahve no proble receiving incoming calls and 3G works fine without any interruption. So to temporalrly solve the problem I move the sim

  • How can i backup itunes my library?

    I don't use "itunes music" folder,my musics are stored in several folders of two disks,i add the folders separatedly, i want to use itunes to manage all the music i've added,and create many playlists in itunes,add many information related to the song

  • Re : Third-Party Order Processing for Subcontracting ( Enhancement Pack 5)

    Hi, In Third-Party Order Processing for subcontracting - Purchase orders for products that are finished by a subcontractor and sent straight to the end customer. PR created in Sales system are processed further in this scenario where you direct your

  • Swf displaying in browser

    When I create an swf file and place in on a web page, how can I get the browsers to show it in the original size?

  • Hda-intel issues

    After some hours of use, this always happens: hda_intel: azx_get_response timeout, switching to polling mode: last cmd=0x018f000a hda_intel: azx_get_response timeout, switching to single_cmd mode: last cmd=0x018f000a and then the headphone jack won't