{"id":26854,"date":"2021-05-03T23:10:04","date_gmt":"2021-05-04T03:10:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/preparedstatements-java-an-example-to-test-the-timing-differences-resulting-from-repeated-raw-queries-vs-repeated-calls-programming-code-examples-java-j2ee-j2me-jdbc\/"},"modified":"2021-05-03T23:10:04","modified_gmt":"2021-05-04T03:10:04","slug":"preparedstatements-java-an-example-to-test-the-timing-differences-resulting-from-repeated-raw-queries-vs-repeated-calls-programming-code-examples-java-j2ee-j2me-jdbc","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26854","title":{"rendered":"PreparedStatements.java  An example to test the timing differences resulting from repeated raw queries vs. repeated calls #Programming Code Examples #Java\/J2EE\/J2ME #JDBC"},"content":{"rendered":"<pre>\npackage cwp;\n\nimport java.sql.*;\n\n\/** An example to test the timing differences resulting\n *  from repeated raw queries vs. repeated calls to\n *  prepared statements. These results will vary dramatically\n *  among database servers and drivers. With my setup\n *  and drivers, Oracle prepared statements took only half\n *  the time that raw queries required when using a modem\n *  connection, and took only 70% of the time that\n *  raw queries required when using a fast LAN connection.\n *  Sybase times were identical in both cases.\n *  <p>\n *\/\n\npublic class PreparedStatements {\n  public static void main(String[] args) {\n    if (args.length  5) &amp;&amp; (args[5].equals(&quot;print&quot;))) {\n      print = true;\n    }\n    Connection connection =\n      getConnection(driver, url, username, password);\n    if (connection != null) {\n      doPreparedStatements(connection, print);\n      doRawQueries(connection, print);\n    }\n  }\n\n  private static void doPreparedStatements(Connection conn,\n                                           boolean print) {\n    try {\n      String queryFormat =\n        &quot;SELECT lastname FROM employees WHERE salary &gt; ?&quot;;\n      PreparedStatement statement =\n        conn.prepareStatement(queryFormat);\n      long startTime = System.currentTimeMillis();\n      for(int i=0; i&lt;40; i++) {\n        statement.setFloat(1, i*5000);\n        ResultSet results = statement.executeQuery();\n        if (print) {\n          showResults(results);\n        }\n      }\n      long stopTime = System.currentTimeMillis();\n      double elapsedTime = (stopTime - startTime)\/1000.0;\n      System.out.println(&quot;Executing prepared statement &quot; +\n                         &quot;40 times took &quot; +\n                         elapsedTime + &quot; seconds.&quot;);\n    } catch(SQLException sqle) {\n      System.out.println(&quot;Error executing statement: &quot; + sqle);\n    }\n  }\n\n  public static void doRawQueries(Connection conn,\n                                  boolean print) {\n    try {\n      String queryFormat =\n        &quot;SELECT lastname FROM employees WHERE salary &gt; &quot;;\n      Statement statement = conn.createStatement();\n      long startTime = System.currentTimeMillis();\n      for(int i=0; i&lt;40; i++) {\n        ResultSet results =\n          statement.executeQuery(queryFormat + (i*5000));\n        if (print) {\n          showResults(results);\n        }\n      }\n      long stopTime = System.currentTimeMillis();\n      double elapsedTime = (stopTime - startTime)\/1000.0;\n      System.out.println(&quot;Executing raw query &quot; +\n                         &quot;40 times took &quot; +\n                         elapsedTime + &quot; seconds.&quot;);\n    } catch(SQLException sqle) {\n      System.out.println(&quot;Error executing query: &quot; + sqle);\n    }\n  } \n\n  private static void showResults(ResultSet results)\n      throws SQLException {\n    while(results.next()) {\n      System.out.print(results.getString(1) + &quot; &quot;);\n    }\n    System.out.println();\n  }\n    \n  private static Connection getConnection(String driver,\n                                          String url,\n                                          String username,\n                                          String password) {\n    try {\n      Class.forName(driver);\n      Connection connection =\n        DriverManager.getConnection(url, username, password);\n      return(connection);\n    } catch(ClassNotFoundException cnfe) {\n      System.err.println(&quot;Error loading driver: &quot; + cnfe);\n      return(null);\n    } catch(SQLException sqle) {\n      System.err.println(&quot;Error connecting: &quot; + sqle);\n      return(null);\n    }\n  }\n\n  private static void printUsage() {\n    System.out.println(&quot;Usage: PreparedStatements host &quot; +\n                       &quot;dbName username password &quot; +\n                       &quot;oracle|sybase [print].&quot;);\n  }\n}\n<\/p><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10210<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, JDBC<br \/>Tags:Java\/J2EE\/J2MEJDBC<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>package cwp; import java.sql.*; \/** An example to test the timing differences resulting * from repeated raw queries vs. repeated calls to * prepared statements. These results will vary dramatically * among database servers and drivers. With my setup * and drivers, Oracle prepared statements took only half * the time that raw queries required &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26854\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26854","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10130,"url":"http:\/\/bangla.sitestree.com\/?p=10130","url_meta":{"origin":26854,"position":0},"title":"PreparedStatements.java An example to test the timing differences resulting from repeated raw queries vs. repeated calls","author":"","date":"August 7, 2015","format":false,"excerpt":"package cwp; import java.sql.*; \/** An example to test the timing differences resulting \u00a0*\u00a0 from repeated raw queries vs. repeated calls to \u00a0*\u00a0 prepared statements. These results will vary dramatically \u00a0*\u00a0 among database servers and drivers. With my setup \u00a0*\u00a0 and drivers, Oracle prepared statements took only half \u00a0*\u00a0 the\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78391,"url":"http:\/\/bangla.sitestree.com\/?p=78391","url_meta":{"origin":26854,"position":1},"title":"Code Refactoring in Java EE","author":"Sayed","date":"August 23, 2025","format":false,"excerpt":"From AI Tools\/OpenAI\/Internet \" Absolutely! Here\u2019s a concise, copyright-free summary of Java EE code refactoring Java EE Code Refactoring: Best Practices Refactoring in Java EE helps improve code quality, maintainability, and performance without changing functionality. Here are some key practices: 1. General Refactoring Remove duplicate code with helper methods or\u2026","rel":"","context":"In &quot;Anything JAVA&quot;","block_context":{"text":"Anything JAVA","link":"http:\/\/bangla.sitestree.com\/?cat=1975"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78491,"url":"http:\/\/bangla.sitestree.com\/?p=78491","url_meta":{"origin":26854,"position":2},"title":"Iterative DNS Resolution Explained","author":"Sayed","date":"September 14, 2025","format":false,"excerpt":"Got it \ud83d\udc4d \u2014 here\u2019s a blog-ready, copyright-free explanation of Iterative Resolution in DNS that you can publish directly. Iterative DNS Resolution Explained When you type a domain name like www.example.com into your browser, the system needs to translate it into an IP address. One way this happens is through\u2026","rel":"","context":"In &quot;Computer Networks&quot;","block_context":{"text":"Computer Networks","link":"http:\/\/bangla.sitestree.com\/?cat=1978"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78493,"url":"http:\/\/bangla.sitestree.com\/?p=78493","url_meta":{"origin":26854,"position":3},"title":"Iterative vs. Recursive DNS Resolution","author":"Sayed","date":"September 14, 2025","format":false,"excerpt":"REF: AI Tools\/Open AI\/ChatGPT (posted as is) \"Perfect follow-up \ud83d\udc4d \u2014 here\u2019s a blog-ready, copyright-free comparison of Iterative vs. Recursive Resolution in DNS: Iterative vs. Recursive DNS Resolution When you enter a domain name like www.example.com, your computer needs its IP address to connect. There are two main ways DNS\u2026","rel":"","context":"In &quot;Computer Networks&quot;","block_context":{"text":"Computer Networks","link":"http:\/\/bangla.sitestree.com\/?cat=1978"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10425,"url":"http:\/\/bangla.sitestree.com\/?p=10425","url_meta":{"origin":26854,"position":4},"title":"The class that actually gets the strings over the network by means of an ObjectInputStream via HTTP tunneling.","author":"","date":"August 28, 2015","format":false,"excerpt":"import java.net.*; import java.io.*; \/** When this class is built, it returns a value \u00a0*\u00a0 immediately, but this value returns false for isDone \u00a0*\u00a0 and null for getQueries. Meanwhile, it starts a Thread \u00a0*\u00a0 to request an array of query strings from the server, \u00a0*\u00a0 reading them in one fell\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10110,"url":"http:\/\/bangla.sitestree.com\/?p=10110","url_meta":{"origin":26854,"position":5},"title":"FruitCreation.java: Creates a simple table named fruits in either an Oracle or a Sybase database.","author":"","date":"August 3, 2015","format":false,"excerpt":"FruitCreation.java Creates a simple table named fruits in either an Oracle or a Sybase database. package cwp; import java.sql.*; \/** Creates a simple table named \"fruits\" in either \u00a0*\u00a0 an Oracle or a Sybase database. \u00a0* \u00a0 \u00a0*\/ public class FruitCreation { \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26854"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26854\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26854"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}