Results 1 to 8 of 8
  1. #1
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default how to run the two queries at the same time

    Final.java

    Java Code:
          int objectId;
          int clientId;
          DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          // String gpstime = null;
          PreparedStatement preparedStatement = null;
          Connection conn=null;
            long Total=0;
            int b;
            int b2;
            int nano=0;
            int nano1=0;
             String disHour="";
             String disMinu="";
             String disSec="";
             Speedfinal f=new Speedfinal();
             ArrayList arr = new ArrayList();
             ArrayList arr1 = new ArrayList();
           try
          {
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn1=DriverManager.getConnection("jdbc:mysql://108.108.8.4:3306/navl?","root", "ajmani@%");
             preparedStatement = conn1.prepareStatement("select gpstime from xydata a inner join fm4features f on a.ID=f.ID where a.objectId= '18'  AND a.clientId = '1' AND a.gpstime >= '2010-05-01 00:00:00'  AND a.gpstime <= '2010-05-25 23:59:59' AND f.DataId='1' AND f.value = '1'");
             rs = preparedStatement.executeQuery();
             long totalTimeInMillis=0;
            
             Set<String> dates = new HashSet<String>();
             ArrayList<Integer> l=new ArrayList<Integer>();
             ArrayList<Integer> m=new ArrayList<Integer>();
             ArrayList<Long> n=new ArrayList<Long>();
             Map <String, Long> datesAndTotal = new HashMap<String, Long>();
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
             Timestamp t1 = null;
             Timestamp t2 = null;
             int count  = 0;
             long sum = 0;
             long result=0;
             long results=0;
             while ( rs.next() )
             {
    
                String gpstime = rs.getString("GpsTime");
                if (dates.add(gpstime))
                {
                   Date d2 = rs.getDate(1);
                   t2 = rs.getTimestamp(1);
                    nano = t2.getSeconds();
                      System.out.println(t2);
                      System.out.println(nano);
                   count++;
                   if (t1 == null )
                   {
                      //nano = t2.getSeconds();
                      //System.out.println(t2);
                      //System.out.println(nano);
                    }
    
                   else
                   {
    
                      totalTimeInMillis = (t2.getTime() - t1.getTime()) / 1000;
                       String dateString = df.format(t2);
                               System.out.println("GPSTime " +gpstime+ " Time in seconds: " + totalTimeInMillis+ " seconds.");
                      if (datesAndTotal.containsKey(dateString))
                      {
                         m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60));
                       
                          for (int y=0; y<m.size();)
                          {
                          sum+=m.get(y);
                          y++;
                        }
                          sum+= (datesAndTotal.get(dateString))/-1;
                          System.out.println(sum);
                          
                      }
                      datesAndTotal.put(dateString, sum);
                   }
                   t1 = t2;
                  
                }
    
    
    
    
             }           
                         List mapKeys = new ArrayList(datesAndTotal.keySet());
                         List mapValues = new ArrayList(datesAndTotal.values());
                         Collections.sort(mapValues);
                          Collections.sort(mapKeys);
                        LinkedHashMap Maps = new LinkedHashMap();
                        Iterator valueIt = mapValues.iterator();
                        while (valueIt.hasNext()) {
                        Object val = valueIt.next();
                         Iterator keyIt = mapKeys.iterator();
                        while (keyIt.hasNext()) {
                        Object key = keyIt.next();
                        if (datesAndTotal.get(key).toString().equals(val.toString()))
                        {
                        datesAndTotal.remove(key);
                        mapKeys.remove(key);
                        Maps.put(key, val);
                        break;
                                   }
                              }
                        }
    
                        
                        Iterator it1 = Maps.entrySet().iterator();
                        String prevKey="";
                        while (it1.hasNext()) {
                           Map.Entry entry = (Map.Entry) it1.next();
                           String presentKey=(String)entry.getKey();
                           if(prevKey.length()==0){
                                   System.out.println(presentKey+"=>"+entry.getValue());
                                   results=(Long) entry.getValue()+60;
                                   System.out.println(results);
                                 arr1.add(presentKey);
                                 int hours = (int) (results / 3600),
                                     remainder = (int) (results % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
                                     
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                if (results==0 ||nano<1)
                                         {
                                            //int se = Integer.parseInt(disSec);
                                            //String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  00 + ":" + 00 + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
                                    System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
                                  
                         
                           }
                           else{
                                   Long present=(Long)entry.getValue();
                                   Long prev=(Long) Maps.get(prevKey);
                                   result=(Long) (present - prev)+30;
                                   System.out.println(""+presentKey+"=>"+result);
                                 System.out.println(result);
                                  arr1.add(presentKey);
                                 int hours = (int) (result / 3600),
                                     remainder = (int) (result % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (result==0||nano < 1)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
                                     
                                   }
                           prevKey=(String)presentKey;
    
                        
                          n.add(result);
                         }
    
                          n.add(results);
    
    
                             for (int i=0; i<n.size();)
                          {
                              Total+=n.get(i);
                              i++;
    
                          }
                             System.out.println(Total);
                                     int hours = (int) (Total / 3600),
                                     remainder = (int) (Total % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                  disHour = (hours < 10 ? "0" : "") + hours;
                                  disMinu = (minutes < 10 ? "0" : "") + minutes;
                                   disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (Total==0||count < 2)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println("Total Time=>" +disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                f.getClass();
                                
                 
          }catch(Throwable th)
    
          {
             Logger.getLogger(testing1.class.getName()).log(Level.SEVERE, null, th);
          }finally
          {
             if(preparedStatement!=null)
                preparedStatement.close();
             if(conn!=null)
                conn.close();
    
          }
          for(int i = 0; i < arr.size(); )
    {  arr.get(i);
       System.out.println(arr1.get(i));
       System.out.println(arr.get(i));
    
      i++;
      }
      
    }
    
    }
    Speedfinal.java
    Java Code:
     int objectId;
          int clientId;
          DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          // String gpstime = null;
          PreparedStatement preparedStatement = null;
          Connection conn=null;
            long Total=0;
            int b;
            int b2;
            int nano=0;
            int nano1=0;
             String disHour="";
             String disMinu="";
             String disSec="";
     ArrayList arr = new ArrayList();
             ArrayList arr1 = new ArrayList();
           try
          {
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn1=DriverManager.getConnection("jdbc:mysql://108.108.8.4:3306/navl?","root", "ajmani@%");
             preparedStatement = conn1.prepareStatement("select gpstime from xydata a where objectId= '18'  AND clientId = '1' AND gpstime >= '2010-05-01 00:00:00'  AND gpstime <= '2010-05-25 23:59:59' AND speed > '3'");
             rs = preparedStatement.executeQuery();
             long totalTimeInMillis=0;
    
             Set<String> dates = new HashSet<String>();
             ArrayList<Integer> l=new ArrayList<Integer>();
             ArrayList<Integer> m=new ArrayList<Integer>();
             ArrayList<Long> n=new ArrayList<Long>();
             Map <String, Long> datesAndTotal = new HashMap<String, Long>();
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
             Timestamp t1 = null;
             Timestamp t2 = null;
             int count  = 0;
             long sum = 0;
             long result=0;
             long results=0;
             while ( rs.next() )
             {
    
                String gpstime = rs.getString("GpsTime");
                if (dates.add(gpstime))
                {
                   Date d2 = rs.getDate(1);
                   t2 = rs.getTimestamp(1);
                    nano = t2.getSeconds();
                      System.out.println(t2);
                      System.out.println(nano);
                   count++;
                   if (t1 == null )
                   {
                      //nano = t2.getSeconds();
                      //System.out.println(t2);
                      //System.out.println(nano);
                    }
    
                   else
                   {
    
                      totalTimeInMillis = (t2.getTime() - t1.getTime()) / 1000;
                       String dateString = df.format(t2);
                               System.out.println("GPSTime " +gpstime+ " Time in seconds: " + totalTimeInMillis+ " seconds.");
                      if (datesAndTotal.containsKey(dateString))
                      {
                         m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60));
    
                          for (int y=0; y<m.size();)
                          {
                          sum+=m.get(y);
                          y++;
                        }
                          sum+= (datesAndTotal.get(dateString))/-1;
                          System.out.println(sum);
    
                      }
                      datesAndTotal.put(dateString, sum);
                   }
                   t1 = t2;
    
                }
    
    
    
    
             }
                         List mapKeys = new ArrayList(datesAndTotal.keySet());
                         List mapValues = new ArrayList(datesAndTotal.values());
                         Collections.sort(mapValues);
                          Collections.sort(mapKeys);
                        LinkedHashMap Maps = new LinkedHashMap();
                        Iterator valueIt = mapValues.iterator();
                        while (valueIt.hasNext()) {
                        Object val = valueIt.next();
                         Iterator keyIt = mapKeys.iterator();
                        while (keyIt.hasNext()) {
                        Object key = keyIt.next();
                        if (datesAndTotal.get(key).toString().equals(val.toString()))
                        {
                        datesAndTotal.remove(key);
                        mapKeys.remove(key);
                        Maps.put(key, val);
                        break;
                                   }
                              }
                        }
    
    
                        Iterator it1 = Maps.entrySet().iterator();
                        String prevKey="";
                        while (it1.hasNext()) {
                           Map.Entry entry = (Map.Entry) it1.next();
                           String presentKey=(String)entry.getKey();
                           if(prevKey.length()==0){
                                   System.out.println(presentKey+"=>"+entry.getValue());
                                   results=(Long) entry.getValue()+60;
                                   System.out.println(results);
                                 arr1.add(presentKey);
                                 int hours = (int) (results / 3600),
                                     remainder = (int) (results % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                if (results==0 ||nano<1)
                                         {
                                            //int se = Integer.parseInt(disSec);
                                            //String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  00 + ":" + 00 + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
                                    System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
    
    
                           }
                           else{
                                   Long present=(Long)entry.getValue();
                                   Long prev=(Long) Maps.get(prevKey);
                                   result=(Long) (present - prev)+30;
                                   System.out.println(""+presentKey+"=>"+result);
                                 System.out.println(result);
                                  arr1.add(presentKey);
                                 int hours = (int) (result / 3600),
                                     remainder = (int) (result % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                 disHour = (hours < 10 ? "0" : "") + hours;
                                 disMinu = (minutes < 10 ? "0" : "") + minutes;
                                 disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (result==0||nano < 1)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
                                    arr.add(disHour + ":" + disMinu + ":"+ disSec);
    
                                   }
                           prevKey=(String)presentKey;
    
    
                          n.add(result);
                         }
    
                          n.add(results);
    
    
                             for (int i=0; i<n.size();)
                          {
                              Total+=n.get(i);
                              i++;
    
                          }
                             System.out.println(Total);
                                     int hours = (int) (Total / 3600),
                                     remainder = (int) (Total % 3600),
                                     minutes = remainder / 60,
                                     seconds = remainder % 60;
    
                                  disHour = (hours < 10 ? "0" : "") + hours;
                                  disMinu = (minutes < 10 ? "0" : "") + minutes;
                                   disSec = (seconds < 10 ? "0" : "") + seconds;
                                       if (Total==0||count < 2)
                                         {
                                            int se = Integer.parseInt(disSec);
                                            String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds();
                                            System.out.println("00" + ":" +  min + ":" + se + " hh:mm:ss");
                                            arr.add(00 + ":" +  00 + ":" + 00);
                                         }
    
                                     System.out.println("Total Time=>" +disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss");
    
    
          }catch(Throwable th)
    
          {
             Logger.getLogger(testing1.class.getName()).log(Level.SEVERE, null, th);
          }finally
          {
             if(preparedStatement!=null)
                preparedStatement.close();
             if(conn!=null)
                conn.close();
    
          }
          for(int i = 0; i < arr.size(); )
    {  arr.get(i);
       System.out.println(arr1.get(i));
       System.out.println(arr.get(i));
    
      i++;
      }
    
    }
    
    }
    both have the same process to take the output but different queries, how to run this into a single file to this two different ouput

    any body please help me

    thanks in advance

    output of final.java
    Java Code:
    2010-05-02=>0
    60
    00:01:00 hh:mm:ss
    2010-05-03=>30
    30
    00:00:30 hh:mm:ss
    2010-05-04=>30
    30
    00:00:30 hh:mm:ss
    2010-05-05=>30
    30
    00:00:30 hh:mm:ss
    2010-05-06=>30
    30
    00:00:30 hh:mm:ss
    2010-05-07=>30
    30
    00:00:30 hh:mm:ss
    2010-05-08=>2866
    2866
    00:47:46 hh:mm:ss
    2010-05-09=>3636
    3636
    01:00:36 hh:mm:ss
    2010-05-10=>30
    30
    00:00:30 hh:mm:ss
    2010-05-11=>17121
    17121
    04:45:21 hh:mm:ss
    2010-05-12=>575
    575
    00:09:35 hh:mm:ss
    2010-05-13=>13482
    13482
    03:44:42 hh:mm:ss
    2010-05-14=>11602
    11602
    03:13:22 hh:mm:ss
    2010-05-15=>10586
    10586
    02:56:26 hh:mm:ss
    2010-05-16=>9066
    9066
    02:31:06 hh:mm:ss
    2010-05-17=>2729
    2729
    00:45:29 hh:mm:ss
    2010-05-18=>3599
    3599
    00:59:59 hh:mm:ss
    2010-05-19=>9206
    9206
    02:33:26 hh:mm:ss
    2010-05-20=>5143
    5143
    01:25:43 hh:mm:ss
    2010-05-21=>2903
    2903
    00:48:23 hh:mm:ss
    2010-05-22=>10615
    10615
    02:56:55 hh:mm:ss
    2010-05-23=>10639
    10639
    02:57:19 hh:mm:ss
    2010-05-24=>14600
    14600
    04:03:20 hh:mm:ss
    2010-05-25=>15012
    15012
    04:10:12 hh:mm:ss
    143620
    Total Time=>39:53:40 hh:mm:ss
    output of speedfinal.java

    Java Code:
    2010-05-02=>0
    60
    00:01:00 hh:mm:ss
    2010-05-03=>30
    30
    00:00:30 hh:mm:ss
    2010-05-04=>30
    30
    00:00:30 hh:mm:ss
    2010-05-05=>30
    30
    00:00:30 hh:mm:ss
    2010-05-06=>30
    30
    00:00:30 hh:mm:ss
    2010-05-07=>30
    30
    00:00:30 hh:mm:ss
    2010-05-08=>1066
    1066
    00:17:46 hh:mm:ss
    2010-05-09=>1162
    1162
    00:19:22 hh:mm:ss
    2010-05-10=>30
    30
    00:00:30 hh:mm:ss
    2010-05-11=>9829
    9829
    02:43:49 hh:mm:ss
    2010-05-12=>240
    240
    00:04:00 hh:mm:ss
    2010-05-13=>7336
    7336
    02:02:16 hh:mm:ss
    2010-05-14=>4942
    4942
    01:22:22 hh:mm:ss
    2010-05-15=>5778
    5778
    01:36:18 hh:mm:ss
    2010-05-16=>2742
    2742
    00:45:42 hh:mm:ss
    2010-05-17=>1417
    1417
    00:23:37 hh:mm:ss
    2010-05-18=>2317
    2317
    00:38:37 hh:mm:ss
    2010-05-19=>5125
    5125
    01:25:25 hh:mm:ss
    2010-05-20=>3234
    3234
    00:53:54 hh:mm:ss
    2010-05-21=>1221
    1221
    00:20:21 hh:mm:ss
    2010-05-22=>6728
    6728
    01:52:08 hh:mm:ss
    2010-05-23=>6700
    6700
    01:51:40 hh:mm:ss
    2010-05-24=>8671
    8671
    02:24:31 hh:mm:ss
    2010-05-25=>10701
    10701
    02:58:21 hh:mm:ss
    79449
    Total Time=>22:04:09 hh:mm:ss
    Last edited by baktha.thalapathy; 06-03-2010 at 07:55 AM.

  2. #2
    viewer is offline Member
    Join Date
    Mar 2010
    Posts
    13
    Rep Power
    0

    Default

    i can not read all of your code...they are so long...
    but i do not understand what you want....
    two output seem same....
    why you need run these gueries same time and create same output same times?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please choose the correct sub-forum next time when you are posting in Java Forums. For this time I'll move it to the correct place.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also your post is too long. Reading the whole is not an easy task, and guess not to anyone here in the forum. So it's better to ask your question more specifically.

    Looking at your title, better to use stored procedures, so you can do the validation in different steps.

  5. #5
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    please check both query are different
    final.java
    Total Time=>39:53:40 hh:mm:ss
    speedfinal.java
    Total Time=>22:04:09 hh:mm:ss


    i need both the output are different java file i need only one java file to run the both query and get two different output

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by baktha.thalapathy View Post
    i need both the output are different java file i need only one java file to run the both query and get two different output

    create two threads in the same java file, one thread runs the first sql and the other the second sql.

    other solution: create one sql that runs with both selects using UNION ALL in sql. here is an example that you can adapt for your tables

    Java Code:
    SELECT 
        *
    FROM 
        (
        SELECT 
            'QUERY1',
            GPSTIME
        FROM 
            GPSTIME 
        WHERE 
            gpstime >= '2010-05-01 00:00:00' 
            AND gpstime <= '2010-05-10 23:59:59' 
            AND speed > '3')
    UNION ALL 
    SELECT 
        *
    FROM 
        (
        SELECT 
            'QUERY2',
            GPSTIME
        FROM 
            GPSTIME 
        WHERE 
            gpstime > '2010-05-10 23:59:59' 
            AND speed = '1')

    you can distinquish the data of the first query because the first attribute has the content QUERY1 and the second QUERY2. you can use different WHERE clauses but the select must have the same number and type of attributes.
    Last edited by j2me64; 06-03-2010 at 02:10 PM.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    First of all you should indent ur code and put comments.Nobody will have time to scan this much big code.
    Another thing is you should not open the resultset and do this much big processing/calculations...it will result to data traffic and resource wastage.
    Ramya:cool:

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    First of all you should indent ur code and put comments.Nobody will have time to scan this much big code.
    Another thing is you should not open the resultset and do this much big processing/calculations...it will result to data traffic and resource wastage.
    I totally agreed with you. Working with data set is a headache most of the time. You must step down the process into smaller as much as possible.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  3. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  4. queries regarding SWT
    By ravian in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 03:06 PM
  5. some queries
    By Feng in forum New To Java
    Replies: 7
    Last Post: 11-24-2007, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •