SolveITProblems is a project of intellectual club

SolveITProblems это проект интеллектуального клуба

понедельник, 22 октября 2012 г.

Create Java web-application with Database Pooling using maven in netbeans

Hi, this article is for guys who knows Java, works with netbeans, understand maven, and needs some examples :)
There are 8 steps to create an java web-application with maven, using netbeans, which will be connected to the database via connection pooling. Also we will show how to use jetty server with maven.

1) Create Maven web-project in netbeans. Yeah, new project->maven->web application
2) If you have problems with memory, its recomended to use not embedded glassfish or tomcat but jetty.
[dont worry after some steps we will show you, how to do the same in tomcat, you'll see that our application will not be ]
You need tom make modifies in pom.xml file:


3) Before run you should create a "WEB-INF" directory under the "Web Pages"
4) And standard deployment descriptor (web.xml) under this directory
[right click on project->Custom->Goals->jetty:run will give a breath to your project]
[if you want to use servlet, you need to define them at web.xml its not a tomcat]
5) So lets go to the database part. First you need to add jdbc driver (again to the pom.xml). In case of oracle you need:


6)We dont need just connection to the database, we need connection pooling
Add this lib to use the ability of connection pooling

Then, as we ue jetty, we need to add datasource for jetty and datasource definition to our application.

6.1)Adding the datasource to jetty:

Create file jetty-env.xml under the WEB-INF and write there:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">

<Configure  class="org.mortbay.jetty.webapp.WebAppContext">

<New id="DSTest" class="">  
     <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                 <Set name="driverClass">oracle.jdbc.driver.OracleDriver</Set>
                 <Set name="jdbcUrl"></Set>
                 <Set name="user">username</Set>
                 <Set name="password">password</Set>

6.2) Adding datasource to our application at web.xml


6.3) Write simple code for checking the datasource

You can add at your servlet:

InitialContext ic = new InitialContext();
                DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DSTest");
                Connection con = ds.getConnection();
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("select * from our_table ");

7) Our web-application is ready. You can build it (right click on app and Build) and run the app (as we do it at step 4)
8) Hm, but what you should do, if you are using tomcat?
Here is the solution:
As the tomcat doesnt understand the jetty-env.xml we need to create a META-INF directory, and context.xml file and write there the following:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/">
  <Resource MaxActive="20" MaxIdle="5" MaxWait="200" auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" name="jdbc/DSTest" password="password" removeAbandonTimeout="120" removeAbandoned="true" type="javax.sql.DataSource" url="" username="username"/>


1 комментарий: