0

I am trying to execute multiple SQL Queries that SELECT different columns from different tables of the same Database. The results will be pasted to an XML. Is there a way were I can merge the queries and produce a single execute statement?

Below you can find the code I use to execute a single query.

String SQLquery1="SELECT * FROM TABLE1";
String SQLquery2="SELECT P.TITLE, P.LASTNAME, P.FIRSTNAME, P.ADDRESS, PO.POSTCODE, P.SEX FROM TABLE2 P LEFT OUTER JOIN TABLE3 PO ON P.VALUE2= PO.VALUE2";
//Code that exececutes a single query
Statement stmt=null;
Connection db = null; 
ResultSet rs=null;
db = DriverManager.getConnection(url);
stmt=db.createStatement();
rs=stmt.executeQuery(SQLquery1);

I was wondering if I can use the executeQuery to execute both of these queries as a single statement or if there is another way to execute them separately. I mean do I have to execute different statements for each query?

5
  • 2
    @PM77-1 executing batch is for 'delete'/'insert'/' update' statements, and not 'select' statements. Commented Nov 28, 2017 at 18:33
  • it doesn't really make sense. how do u expect your result set to work? Commented Nov 28, 2017 at 18:44
  • @isaace I was wondering if I can include them everything in a single ResultSet. Commented Nov 28, 2017 at 18:45
  • 1
    but the 2 queries return different result sets with different columns names and data types. If they return the exact same data types with column names then you can use union to make it into 1 query. Commented Nov 28, 2017 at 18:47
  • @isaace Yeah that makes perfect sense now...I will use the same statement with different ResultSets each time. Commented Nov 28, 2017 at 18:48

1 Answer 1

1

I mean do I have to execute different statements for each query?

You do. If you were selecting the same columns in both queries (or at least the same # and type of columns), you could combine the queries with a UNION ALL. Since the columns differ, you must use different statements (and connections - if you want to run both queries simultaneously). If you run the queries sequentially, one connection is sufficient.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.