468,305 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,305 developers. It's quick & easy.

Execution of multiple select statements

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 java.io.*;    
  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+ " ");
  16.  
  17.        }
  18.       while(rs.next())
  19.      {
  20.         System.out.println("\n"+rs.getString(1)+ " " +rs.getString(2));
  21.      }
  22.  
  23.  }
  24. }
Aug 25 '13 #1
4 9019
Rabbit
12,512 Expert Mod 8TB
Why can't you run different queries? Any other workaround would require more work than just running separate queries.
Aug 25 '13 #2
p9878
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
chaarmann
785 Expert 512MB
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
Sherin
77 64KB
Try This Code
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3. public class jdbcConn {
  4.    public static void main(String[] args) throws Exception{
  5.       Class.forName("org.apache.derby.jdbc.ClientDriver");
  6.       Connection con = DriverManager.getConnection
  7.       ("jdbc:derby://localhost:1527/testDb","name","pass");
  8.  
  9.       Statement stmt = con.createStatement
  10.       (ResultSet.TYPE_SCROLL_SENSITIVE,
  11.       ResultSet.CONCUR_UPDATABLE);
  12.       String insertEmp1 = "insert into emp values
  13.       (10,'jay','trainee')";
  14.       String insertEmp2 = "insert into emp values
  15.       (11,'jayes','trainee')";
  16.       String insertEmp3 = "insert into emp values
  17.       (12,'shail','trainee')";
  18.       con.setAutoCommit(false);
  19.       stmt.addBatch(insertEmp1);//inserting Query in stmt
  20.       stmt.addBatch(insertEmp2);
  21.       stmt.addBatch(insertEmp3);
  22.       ResultSet rs = stmt.executeQuery("select * from emp");
  23.       rs.last();
  24.       System.out.println("rows before batch execution= "
  25.       + rs.getRow());
  26.       stmt.executeBatch();
  27.       con.commit();
  28.       System.out.println("Batch executed");
  29.       rs = stmt.executeQuery("select * from emp");
  30.       rs.last();
  31.       System.out.println("rows after batch execution= "
  32.       + rs.getRow());
  33.    }
Nov 11 '20 #5

Post your reply

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

Similar topics

24 posts views Thread by sureshjayaram | last post: by
3 posts views Thread by Joe via DotNetMonster.com | last post: by
14 posts views Thread by Michel Esber | last post: by
2 posts views Thread by =?Utf-8?B?VGVycnk=?= | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.