'mySQL java union

i used this query java and mysql but its not work (if i send directly mysql console its work bun in java not work.)

Here down is my code:

PreparedStatement pst;
ResultSet rs;
String Sql6 ="(SELECT a,b,c  FROM T1 where a not in(0)  ORDER BY a desc LIMIT 5)  
              UNION   
              (SELECT e,f,g FROM T2 where e not in(0) ORDER BY e desc LIMIT 5)";

pst = this.getC().prepareStatement(sql6);
System.out.println(    "===========2"); //its work this line
rs = pst.executeQuery();
System.out.println(    "===========3"); //its not work this line

Where is the problem?

thank you



Solution 1:[1]

if you use UNION in java jdbc , you attention to outcome. out come data column is only first column name

(SELECT t1id,t1name,t1type FROM t1 ORDER BY t1id desc limit 5)  
UNION
(SELECT t2id,t2color,t2place FROM t2 ORDER BY t2id  desc LIMIT 5)

outcome are t1id ->(t1id & t2id) t1name ->(t1name & t2color) t1type -> (t1type & t2place) ... both of select one list

its work in java jdbc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1