473,378 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 11419
Rabbit
12,516 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

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

Similar topics

0
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure ....
24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
1
by: Harry V | last post by:
I'm wondering if there is a limit of a single select, delete and insert statement (command) to each dataadapter? On a form, I have a dataconnection that is shared by 3 dataadapters, one for each of...
3
by: Joe via DotNetMonster.com | last post by:
Hi, I'm trying to use several select statements so that I don't need to call the function several times. The next Result set always seems to read the first select statement. I have the...
14
by: Michel Esber | last post by:
Linux RH 4.0 running DB2 V8 FP 11. I have a table with ~ 11M rows and running DELETE statements is really slow. Deleting 1k rows takes more than 3 minutes. If I run select statements on the same...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
4
by: vertigo262 | last post by:
Is it possible to use to select statements in a stored procedure? I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings. The code...
7
by: golffor1 | last post by:
Hello I was wondering if you could help me out with doing multiple select statements. I have this objCommand.CommandText = "Select * from table" objCommand.CommandText1 = "Select * from...
3
by: els12 | last post by:
How do I write 2 distinct select queries in one sql statement, and then also divide the results of one by the other? The first query will give me a subset of all the records in the table...the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.