Displaying differences for changeset
 
display as  

test/TestDriver.java

@@ -6,9 +6,11 @@
 
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.ArrayList;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ThreadLocalRandom;
@@ -33,6 +35,7 @@
     private static Connection conn;
     //private static ArrayList<RandomMapunit> testMapunits = new ArrayList<>();
     private static Queue<RandomMapunit> testMapunits = new ConcurrentLinkedQueue<>();
+    private static Queue<RandomMapunit> randomMapunits = new ConcurrentLinkedQueue<>();
 
     /**
      * Number of random mapunits to test intersections for.
@@ -114,7 +117,8 @@
         }
     }
 
-    @Test(threadPoolSize = 10, invocationCount = MAX_RANDOM_MAPUNITS, timeOut = 0)
+    
+    @Test(enabled=true, threadPoolSize = 10, invocationCount = MAX_RANDOM_MAPUNITS, timeOut = 0)
     public void testComplexIntersectionQuery() throws InterruptedException {
         String bufferWKT;
 
@@ -149,6 +153,7 @@
                 results = statement.executeQuery(query);
                 double area = 0.0;
                 System.out.println(Thread.currentThread().getId() + ":  Query took " + ((System.currentTimeMillis()) - timeMills) / 1000.0 + " seconds.");
+                System.out.flush();
                 while (results.next()) {
                     String areasymbol = results.getString("areasymbol");
                     String musym = results.getString("musym");
@@ -169,6 +174,56 @@
         }
     }
 
+    @Test(enabled=true, threadPoolSize = 10, invocationCount = MAX_RANDOM_MAPUNITS, timeOut = 0)
+    public void testPreparedStatementQuery() throws InterruptedException {
+
+        RandomMapunit tMapunit = randomMapunits.poll();
+        long sleepTime = ThreadLocalRandom.current().nextInt(10, 2000);
+        //System.out.println(Thread.currentThread().getId() + ":  Sleepting for " + sleepTime + " milliseconds.");
+        //System.out.flush();
+        Thread.sleep(sleepTime);
+        if (null != tMapunit) {
+            ArrayList<Object> values = new ArrayList<>();
+            values.add(Integer.parseInt(tMapunit.mukey));
+            values.add(tMapunit.areasymbol);
+            values.add(Integer.parseInt(tMapunit.mupolygonKey));
+            System.out.println(Thread.currentThread().getId() + ":  Runing prepared statement query for mupolygonkey: " + tMapunit.mupolygonKey);
+            System.out.flush();            
+            String query = "SELECT mupolygon.mukey FROM mupolygon "
+                    + " INNER JOIN mapunit ON mupolygon.mukey=mapunit.mukey "
+                    + " INNER JOIN legend ON mapunit.lkey=legend.lkey "
+                    + " WHERE mapunit.mukey=? AND legend.areasymbol=? AND mupolygon.mupolygonkey=?;";
+
+            try {
+                ResultSet results;
+                long timeMills = System.currentTimeMillis();                
+                PreparedStatement statement = conn.prepareStatement(query);
+                for (int i = 0; i < values.size(); i++) {
+                    statement.setObject(i + 1, values.get(i));
+                }
+
+                results = statement.executeQuery();
+                
+                System.out.println(Thread.currentThread().getId() + ":  Query took " + ((System.currentTimeMillis()) - timeMills) / 1000.0 + " seconds.");
+                System.out.flush();                
+                if (results.next()) {
+                    Assert.assertEquals(tMapunit.mukey, results.getString("mukey"), "Results did not find the correct mupolygonkey." );
+
+                } else {
+                    Assert.assertTrue(false, "Could not retrieve the record for this mukey using a prepared statement: " + tMapunit.mukey);
+                }
+
+            } catch (SQLException ex) {
+                Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex);
+                Assert.assertTrue(false, "Could not use the SDMPreparedStatement: " + ex.getMessage());
+            }
+
+        }else {
+            System.out.println("Queue is empty.  Finished.");
+        }
+
+    }
+
     @BeforeClass
     public static void setUpClass() throws Exception {
         System.out.println("Loading the SDMDriver, and testing a connection.");
@@ -199,6 +254,8 @@
                 System.out.println("Got mupolygonkey: " + results.getString("mupolygonkey"));
                 RandomMapunit tMapunit = new RandomMapunit(results.getString("mupolygonkey"), results.getString("mukey"), results.getString("areasymbol"), results.getString("centerPoint"));
                 testMapunits.add(tMapunit);
+                randomMapunits.add(tMapunit);
+
             }
         } catch (SQLException ex) {
             Logger.getLogger(TestDriver.class.getName()).log(Level.SEVERE, null, ex);