Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is probably not a good solution that you are having here. When you are using servlets in order to do something your application loads, you can have listeners. What you have to do is, first create a class implementing <a href="http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContextListener.html" rel="nofollow">ServletContextListener</a>.</p> <pre><code>public class ContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { ServletContext context=sce.getServletContext(); String dburl=context.getInitParameter("dbUrl"); String dbusername=context.getInitParameter("dbUserName"); String dbpassword=context.getInitParameter("dbPassword"); DBConnector.createConnection(dburl, dbusername, dbpassword); System.out.println("Connection Establised........."); } public void contextDestroyed(ServletContextEvent sce) { DBConnector.closeConnection(); } } </code></pre> <p>After that you can create another class to create the connection when application loads.</p> <pre><code>public class DBConnector { private static Connection con; public static void createConnection(String dbUrl,String dbusername,String dbPassword){ try { Class.forName("org.postgresql.Driver"); con=DriverManager.getConnection(dbUrl, dbusername, dbPassword); } catch (Exception ex) { ex.printStackTrace(); } } public static Connection getConnection(){ return con; } public static void closeConnection(){ if(con!=null){ try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } } </code></pre> <p>Then when ever you want to get the db connection you can call the static getConnection method and get it. In order to call your listener class you have to add the listener tags inside the web.xml.</p> <pre><code>&lt;context-param&gt; &lt;param-name&gt;dbUrl&lt;/param-name&gt; &lt;param-value&gt;jdbc:postgresql://localhost:5432/jbossdb&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;dbUserName&lt;/param-name&gt; &lt;param-value&gt;qwerty&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;dbPassword&lt;/param-name&gt; &lt;param-value&gt;qwerty&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;com.icbt.bookstore.listener.ContextListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>It is not good practice to hardcode your db usernames and passwords. Instead you can use servlet init parameters in the web.xml as shown.</p> <p>Hope, this answers your question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload