{"id":10116,"date":"2015-08-05T02:55:04","date_gmt":"2015-08-05T06:55:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10116"},"modified":"2015-08-04T10:23:08","modified_gmt":"2015-08-04T14:23:08","slug":"dbresults-java-class-to-store-completed-results-of-a-jdbc-query-differs-from-a-resultset-in-several-ways","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10116","title":{"rendered":"DBResults.java: Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways"},"content":{"rendered":"<pre># DBResults.java\u00a0 Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways:\r\n\r\n\u00a0\u00a0\u00a0 * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows.\r\n\u00a0\u00a0\u00a0 * This class stores results as strings, in arrays.\r\n\u00a0\u00a0\u00a0 * This class includes DatabaseMetaData (database product name and version) and ResultSetMetaData (the column names).\r\n\u00a0\u00a0\u00a0 * This class has a toHTMLTable method that turns the results into a long string corresponding to an HTML table.<\/pre>\n<hr \/>\n<pre>\r\npackage cwp;\r\n\r\nimport java.sql.*;\r\nimport java.util.*;\r\n\r\n\/** Class to store completed results of a JDBC Query.\r\n\u00a0*\u00a0 Differs from a ResultSet in several ways:\r\n\u00a0* \u00a0\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 ResultSet doesn't necessarily have all the data;\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 reconnection to database occurs as you ask for\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 later rows.\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 This class stores results as strings, in arrays.\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 This class includes DatabaseMetaData (database product\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 name and version) and ResultSetMetaData\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (the column names).\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 This class has a toHTMLTable method that turns\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the results into a long string corresponding to\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 an HTML table.\r\n\u00a0\u00a0\u00a0\u00a0 * \u00a0\r\n\u00a0* \u00a0\r\n\u00a0*\/\r\n\r\npublic class DBResults {\r\n\u00a0 private Connection connection;\r\n\u00a0 private String productName;\r\n\u00a0 private String productVersion;\r\n\u00a0 private int columnCount;\r\n\u00a0 private String[] columnNames;\r\n\u00a0 private Vector queryResults;\r\n\u00a0 String[] rowData;\r\n\r\n\u00a0 public DBResults(Connection connection,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String productName,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String productVersion,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int columnCount,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String[] columnNames) {\r\n\u00a0\u00a0\u00a0 this.connection = connection;\r\n\u00a0\u00a0\u00a0 this.productName = productName;\r\n\u00a0\u00a0\u00a0 this.productVersion = productVersion;\r\n\u00a0\u00a0\u00a0 this.columnCount = columnCount;\r\n\u00a0\u00a0\u00a0 this.columnNames = columnNames;\r\n\u00a0\u00a0\u00a0 rowData = new String[columnCount];\r\n\u00a0\u00a0\u00a0 queryResults = new Vector();\r\n\u00a0 }\r\n\r\n\u00a0 public Connection getConnection() {\r\n\u00a0\u00a0\u00a0 return(connection);\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 public String getProductName() {\r\n\u00a0\u00a0\u00a0 return(productName);\r\n\u00a0 }\r\n\r\n\u00a0 public String getProductVersion() {\r\n\u00a0\u00a0\u00a0 return(productVersion);\r\n\u00a0 }\r\n\r\n\u00a0 public int getColumnCount() {\r\n\u00a0\u00a0\u00a0 return(columnCount);\r\n\u00a0 }\r\n\r\n\u00a0 public String[] getColumnNames() {\r\n\u00a0\u00a0\u00a0 return(columnNames);\r\n\u00a0 }\r\n\r\n\u00a0 public int getRowCount() {\r\n\u00a0\u00a0\u00a0 return(queryResults.size());\r\n\u00a0 }\r\n\r\n\u00a0 public String[] getRow(int index) {\r\n\u00a0\u00a0\u00a0 return((String[])queryResults.elementAt(index));\r\n\u00a0 }\r\n\r\n\u00a0 public void addRow(String[] row) {\r\n\u00a0\u00a0\u00a0 queryResults.addElement(row);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Output the results as an HTML table, with\r\n\u00a0\u00a0 *\u00a0 the column names as headings and the rest of\r\n\u00a0\u00a0 *\u00a0 the results filling regular data cells.\r\n\u00a0\u00a0 *\/\r\n\u00a0 \r\n\u00a0 public String toHTMLTable(String headingColor) {\r\n\u00a0\u00a0\u00a0 StringBuffer buffer =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new StringBuffer(\"\\n\");\r\n\u00a0\u00a0\u00a0 if (headingColor != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 buffer.append(\"\u00a0 \\n\u00a0\u00a0\u00a0 \");\r\n\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 buffer.append(\"\u00a0 \\n\u00a0\u00a0\u00a0 \");\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 for(int col=0; col\" + columnNames[col]);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 for(int row=0; row\\n\u00a0\u00a0\u00a0 \");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String[] rowData = getRow(row);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int col=0; col\" + rowData[col]);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 buffer.append(\"\\n\");\r\n\u00a0\u00a0\u00a0 return(buffer.toString());\r\n\u00a0 } \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p># DBResults.java\u00a0 Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways: \u00a0\u00a0\u00a0 * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows. \u00a0\u00a0\u00a0 * This class stores results as strings, in arrays. \u00a0\u00a0\u00a0 * This class includes DatabaseMetaData (database product &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10116\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,1449,308,1446,1450],"class_list":["post-10116","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-db","tag-java","tag-jdbc","tag-query","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26830,"url":"http:\/\/bangla.sitestree.com\/?p=26830","url_meta":{"origin":10116,"position":0},"title":"DBResults.java: Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"# DBResults.java Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways: * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows. * This class stores results as strings, in arrays. * This class includes DatabaseMetaData\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26854,"url":"http:\/\/bangla.sitestree.com\/?p=26854","url_meta":{"origin":10116,"position":1},"title":"PreparedStatements.java  An example to test the timing differences resulting from repeated raw queries vs. repeated calls #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10106,"url":"http:\/\/bangla.sitestree.com\/?p=10106","url_meta":{"origin":10116,"position":2},"title":"FruitTest.java: A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the &#8220;fruits&#8221; table.","author":"","date":"August 2, 2015","format":false,"excerpt":"# FruitTest.java\u00a0 A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the \"fruits\" table. package cwp; import java.sql.*; \/** A JDBC example that connects to either an Oracle or \u00a0*\u00a0 a Sybase database and prints out the values\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":26822,"url":"http:\/\/bangla.sitestree.com\/?p=26822","url_meta":{"origin":10116,"position":3},"title":"FruitTest.java:  A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the &quot;fruits&quot; table. #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"# FruitTest.java A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the \"fruits\" table. package cwp; import java.sql.*; \/** A JDBC example that connects to either an Oracle or * a Sybase database and prints out the values\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26828,"url":"http:\/\/bangla.sitestree.com\/?p=26828","url_meta":{"origin":10116,"position":4},"title":"DatabaseUtilities.java: Several general-purpose utilities discussed and used in the chapter. #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"package cwp; import java.sql.*; \/** Three database utilities: * 1) getQueryResults. Connects to a database, executes * a query, retrieves all the rows as arrays * of strings, and puts them inside a DBResults * object. Also places the database product name, * database version, and the names of all\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10113,"url":"http:\/\/bangla.sitestree.com\/?p=10113","url_meta":{"origin":10116,"position":5},"title":"DatabaseUtilities.java: Several general-purpose utilities discussed and used in the chapter.","author":"","date":"August 4, 2015","format":false,"excerpt":"package cwp; import java.sql.*; \/** Three database utilities: \u00a0*\u00a0\u00a0 1) getQueryResults. Connects to a database, executes \u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 a query, retrieves all the rows as arrays \u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 of strings, and puts them inside a DBResults \u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 object. Also places the database product name, \u00a0*\u00a0\u00a0\u00a0\u00a0\u00a0 database version, and the names of all\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\/10116","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\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10116"}],"version-history":[{"count":4,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10116\/revisions"}],"predecessor-version":[{"id":10627,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10116\/revisions\/10627"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}