By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

Execution of multiple select statements

P: 2
I want to execute multiple select statements in a single query to find frequency of values in a database
I have tried this:
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2. import*;    
  3. class t1
  4. {
  5.   public static void main(String a[])throws Exception
  6.  {
  7.       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  8.       Connection con=DriverManager.getConnection("jdbc:odbc:mydsn");
  9.       Statement s=con.createStatement();
  10.       ResultSet rs=s.executeQuery("select Platform,count(*) from Table11 group by Platform;select Job,count(*) from Table11 group by Job");
  11.       ResultSetMetaData r=rs.getMetaData();
  12.       for(int i=1;i<=r.getColumnCount();i++)
  13.        {
  14.          String c=r.getColumnLabel(i);
  15.          System.out.print(c+ " ");
  17.        }
  18.       while(
  19.      {
  20.         System.out.println("\n"+rs.getString(1)+ " " +rs.getString(2));
  21.      }
  23.  }
  24. }
Aug 25 '13 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 10K+
P: 12,421
Why can't you run different queries? Any other workaround would require more work than just running separate queries.
Aug 25 '13 #2

P: 2
I think it becomes lenghthy.I want to find frequency of 10 attributes in a database.
can't we write multiple queries in a single resultset?
Aug 26 '13 #3

Expert 100+
P: 785
You can combine results of multiple queries with "union".
If you need to differentiate later on from which query the result came from:
Expand|Select|Wrap|Line Numbers
  1. select value1 as result1, null as result2 from table1 where ...
  2. union
  3. select null as result1, value2 as result2 from table2 where ...
or use an additinal field:
Expand|Select|Wrap|Line Numbers
  1. select "1" as queryNumber, value1 as result from table1 where ...
  2. union
  3. select "2" as queryNumber, value2 as result from table2 where ...
But as Rabbit statet, most time it's better to run 2 queries. You must weight the connection costs, response time and memory usage dependent of the expected results. If you expect several hundred records, use 2 queries. if you run over several tables but only expect a single record back, use combined query.
Aug 26 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.