{"id":20,"date":"2013-06-26T15:46:21","date_gmt":"2013-06-26T15:46:21","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=20"},"modified":"2013-06-26T15:54:15","modified_gmt":"2013-06-26T15:54:15","slug":"using-singleton-class-for-database-connectivity-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/using-singleton-class-for-database-connectivity-java\/","title":{"rendered":"Using Singleton class for Database Connectivity Java"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Database \r\n{\r\n    private static DataSource dataSource;\r\n\r\n    static \r\n    {\r\n        try \r\n        {\r\n          dataSource = new InitialContext().lookup(&quot;jndifordbconc&quot;);\r\n        }catch (NamingException e) \r\n        { \r\n          throw new ExceptionInInitializerError(&quot;'jndifordbconc' not found in JNDI&quot;, e);\r\n        }\r\n    }\r\n\r\n    public static Connection getConnection() \r\n    {\r\n      return dataSource.getConnection();\r\n    }\r\n}\r\n\r\npublic List&lt;Entity&gt; list() throws SQLException \r\n{\r\n    Connection        connection = null;\r\n    PreparedStatement statement  = null;\r\n    ResultSet         resultSet  = null;\r\n    List&lt;Entity&gt;      entities   = new ArrayList&lt;Entity&gt;();\r\n\r\n    try \r\n    {\r\n        connection = Database.getConnection();\r\n        statement  = connection.prepareStatement(&quot;SELECT id, foo, bar FROM entity&quot;);\r\n        resultSet  = statement.executeQuery();\r\n\r\n        while (resultSet.next()) \r\n        {\r\n            Entity entity = new Entity();\r\n            entity.setId(resultSet.getLong(&quot;id&quot;));\r\n            entity.setFoo(resultSet.getString(&quot;foo&quot;));\r\n            entity.setBar(resultSet.getString(&quot;bar&quot;));\r\n            entities.add(entity);\r\n         }\r\n    } \r\n    finally \r\n    {\r\n        if (resultSet  != null) try { resultSet.close();  } catch (SQLException ignore) {}\r\n        if (statement  != null) try { statement.close();  } catch (SQLException ignore) {}\r\n        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}\r\n    }\r\n    return entities;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public class Database { private static DataSource dataSource; static { try { dataSource = new InitialContext().lookup(&quot;jndifordbconc&quot;); }catch (NamingException e) { throw new ExceptionInInitializerError(&quot;&#8217;jndifordbconc&#8217; not found in JNDI&quot;, e); } } public static Connection getConnection() { return dataSource.getConnection(); } } public List&lt;Entity&gt; list() throws SQLException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet&hellip; <a href=\"https:\/\/codethataint.com\/blog\/using-singleton-class-for-database-connectivity-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}