如何使用java从oracle数据库中随机选择图像

时间:2013-06-12 08:54:56

标签: java oracle

我想将图像及其信息随机显示在网页上。每隔一段时间后,它应该自动更改图像并在浏览器中查看。我怎么能用java和oracle数据库做到这一点 我只是尝试创建一个计时器,然后尝试调用以下查询,但它不起作用

SELECT * FROM ( SELECT name FROM employee ORDER BY dbms_random.value ) WHERE rownum <= 1000

&安培;然后在计时器运行方法中我像这样调用这个查询。

  TimerTask task = new TimerTask(){
    public void run()
    {
     //execute the code for every 10 mins

       Timer timer = new Timer();


      timer.scheduleAtFixedRate(task, 0, 10 * 60 * 1000); // period is  10 mins 

       try
      {

         Class.forName("oracle.jdbc.driver.OracleDriver");
         Connection con=DriverManager.getConnection("jdbc:oracle:thin:
         @localhost:1521:orcl","Admin","secret");
         Statement st=con.createStatement();
         System.out.println("connection established successfully...!!");     

         ResultSet rs=st.executeQuery("SELECT  *\n" +
  "FROM    (\n" +
   "        SELECT  name\n" +
     "        FROM    employee\n" +
    "        ORDER BY\n" +
   "                dbms_random.value\n" +
    "        )\n" +
   "WHERE rownum <= 1000");

     //   pw.println("<table border=1>");
             while(rs.next())
             {

            System.out.println("<br>"+"Id :"+rs.getInt("name"));
             }
//         pw.println("</table>"); 
    }
    catch (Exception e){
        e.printStackTrace();
    }
}
    };

1 个答案:

答案 0 :(得分:1)

步骤1:触发SQL查询以从图像表中获取ID

select image_id from ImageTable ;

第2步:在某些java列表中存储id的值

ArrayList<Integer> IdList = new ArrayList<Integer>();
while(rs.next())
{
idList.add(rs.getString(1));
}

步骤3:编写一个函数,从给定列表中生成随机数

int randomIdMethod(ArrayList<Integer> idList)
{
// logic
return randomImageId;
}

步骤4:将id列表传递给随机id生成器方法

步骤5:检索从randomIdmethod

返回的随机ID
Strnig newQuery = "select image fom ImageTable where imageId=randomImageId" ;

步骤6:从图像表中获取图像,并将is作为参数