Samples JDK
NestedQueryTest.java
1 package com.freemindcafe.jdbc.sample4;
2 
3 import java.io.IOException;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.List;
8 
9 import junit.framework.Assert;
10 
11 import org.apache.commons.dbcp.BasicDataSource;
12 import org.junit.Test;
13 import org.springframework.dao.DataAccessException;
14 import org.springframework.jdbc.core.JdbcTemplate;
15 import org.springframework.jdbc.core.ResultSetExtractor;
16 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
17 import org.springframework.transaction.TransactionStatus;
18 import org.springframework.transaction.support.TransactionCallback;
19 import org.springframework.transaction.support.TransactionTemplate;
20 
21 public class NestedQueryTest {
22 
23  @Test
24  public void test() throws SQLException, IOException{
25 
26  BasicDataSource dataSource=new BasicDataSource();
27  dataSource.setUrl("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=XE)))");
28  dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
29  dataSource.setUsername("system");
30  dataSource.setPassword("neha1");
31  dataSource.setMaxActive(2);
32  dataSource.setMaxIdle(2);
33  dataSource.setMaxOpenPreparedStatements(2);
34  dataSource.setMaxWait(1000l);
35  dataSource.setPoolPreparedStatements(true);
36  final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
37  final List<Integer> list=new ArrayList<Integer>();
38  String query="Select 1 a from dual where 2="
39  + "(select 2 from dual where 3="
40  + "(select 3 from dual where 4="
41  + "(select 4 from dual)))";
42  Boolean exception=false;
43 
44  try{
45 
46  jdbcTemplate.query(query, new ResultSetExtractor<Object> (){
47 
48  public Object extractData(ResultSet rs) throws SQLException,
49  DataAccessException {
50  if (rs.next())
51  {
52  jdbcTemplate.query("select 2 from dual", new ResultSetExtractor<Object> (){
53 
54  public Object extractData(ResultSet arg0)
55  throws SQLException, DataAccessException {
56  if (arg0.next()){
57  jdbcTemplate.query("select 3 from dual", new ResultSetExtractor<Object> (){
58 
59  public Object extractData(ResultSet arg0)
60  throws SQLException, DataAccessException {
61  if (arg0.next()){
62  list.add(arg0.getInt(1));
63  }
64  return true;
65  }});
66 
67  return new Boolean(true);
68  }
69  return true;
70  }});
71 
72  return new Boolean(true);
73  }
74  return true;
75  }
76 
77  });
78  }catch(Exception ex){
79  exception = true;
80  }
81  Assert.assertTrue(exception);
82 
83 
84  TransactionTemplate transactionTemplate=new TransactionTemplate();
85  DataSourceTransactionManager transactionManager =new DataSourceTransactionManager(dataSource);
86  transactionTemplate.setTransactionManager(transactionManager );
87 
88  transactionTemplate.execute(new TransactionCallback<Object>() {
89 
90  public Object doInTransaction(TransactionStatus arg0) {
91  return jdbcTemplate.query("select 1 from dual", new ResultSetExtractor<Object> (){
92  public Object extractData(ResultSet rs) throws SQLException,DataAccessException {
93 
94  if (rs.next())
95  {
96  jdbcTemplate.query("select 2 from dual", new ResultSetExtractor<Object> (){
97 
98  public Object extractData(ResultSet arg0)
99  throws SQLException, DataAccessException {
100  if (arg0.next()){
101  jdbcTemplate.query("select 3 from dual", new ResultSetExtractor<Object> (){
102 
103  public Object extractData(ResultSet arg0)
104  throws SQLException, DataAccessException {
105  if (arg0.next()){
106  list.add(arg0.getInt(1));
107  }
108  return true;
109  }});
110 
111  return new Boolean(true);
112  }
113  return true;
114  }});
115 
116  return new Boolean(true);
117  }
118  return true;
119  }
120 
121  });
122  }
123  });
124 
125  Assert.assertEquals(list.size(), 1);
126 
127  Assert.assertEquals((Integer)list.get(0), new Integer(3));
128  }
129 }