Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem to be talking about using directly a database connection from your JSP files, don't you? Well, this is not a theoretically correct solution because following separation of concerns principle, your view -your JSP file- shouldn't know anything at all about databases or data access. Check any description of MVC pattern, for instance this pretty obvious ;-):</p> <p><a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow noreferrer">Wikipedia on MVC pattern</a></p> <p>But if you want to take the shorcut and have your view aware of your database you have, in my opinion, two options, both of them involving the JNDI lookup of a datasource previously defined in your appplication as <a href="https://stackoverflow.com/users/1193148/marcelo-tataje">Marcelo Tataje</a> already said. </p> <p>On how to define on Tomcat a JNDI datasource based on a connection pool:</p> <p><a href="http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html" rel="nofollow noreferrer">http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html</a></p> <p>In the same page you can see how to use this connection pool to issue queries right from your JSP (again very inadvisable in my opinion)</p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;sql:query var="rs" dataSource="jdbc/TestDB"&gt; select id, foo, bar from testdata &lt;/sql:query&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;DB Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;Results&lt;/h2&gt; &lt;c:forEach var="row" items="${rs.rows}"&gt; Foo ${row.foo}&lt;br/&gt; Bar ${row.bar}&lt;br/&gt; &lt;/c:forEach&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><a href="https://stackoverflow.com/questions/1531073/is-java-sql-connection-thread-safe">Is java.sql.Connection thread safe?</a></p> <p>The other slightly better solution is, in your java code, JNDI lookup the datasource like this from your context listener:</p> <pre><code> public void contextInitialized(ServletContextEvent contextEvent) { // JNDI Datasource lookup InitialContext context = new InitialContext(); DataSource dataSource = (DataSource) context .lookup("jdbc/DataSource"); // Storing datasource in application context contextEvent.getServletContext().setAttribute("datasource",dataSource); } </code></pre> <p>and after from any servlet use that stored datasource to get the database connection you need like this </p> <pre><code>((DataSource)contextEvent.getServletContext().getAttribute("datasource")).getConnection() </code></pre> <p>Don't use Connection class directly please. It's awfully primitive and obsolete. Nowadays nobody in a serious working environment handle connections like that. If you do what <a href="https://stackoverflow.com/users/1115584/amitg">AmitG</a> proposed you will end up with an unique connection shared by all concurrent accesses. See this on why this isn't the way to go</p>
    singulars
    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. 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