2

I am new in servlet and database.I have a core.java file in which I have a JDBC connection code.In the same directory I have a coreServlet.java file which take form value from HTML page.My questions are:

  1. How can I make interaction between two java classes,for example how can I use variables that I got from HTML form in servlet(coreServlet.java) to my core.java file so that I can store that in my database ?
  2. Is it is a better Idea to put my jdbc connection code in coreServlet.java ?

My project structure is like:

  - aarya(project name)   
        |
         - WEB-INF
                 |
        |         -web.xml
        -src
           |
          -pkg
           |
            -CoreServlet.java(servlet to interact HTML form)
            -Main.java
            -Core.java(jdbc code is here to interact database)
        |
        -html
            |
            - core.html
        |
        - css
             |
             -core.css

        |
        -javascript
                  |
                   -core.js
        |
        - lib
        |
        -index.html
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Anshul
  • 9,312
  • 11
  • 57
  • 74
  • 1) you package should be in reverse url format, `com.techdon` for example. 2) you classes should start with a capital letter as per naming conventions. 3) This isn't really an SO question, this site is for concrete problems not open ended questions. – Boris the Spider Apr 12 '13 at 21:41
  • @bmorris591 is not an open ended question. – Luiggi Mendoza Apr 12 '13 at 21:42
  • @bmorris591,@Luiggi I made required changes,hoping answer from you... – Anshul Apr 12 '13 at 21:49

1 Answers1

2

The database connection data should be in your JNDI Data Source and it would be better if you use a Connection Pool but never in a class. Since yours is a Web Application, note that the connection pool configuration depends heavily on the web application server.

As examples, this is very well explained in Tomcat 7 Database Connection Pool configuration and JBoss 7 Database Connection Pool configuration (there are other steps to configure the database connection pool on GlassFish and other Web application server, note that this is different on each server).

From both examples, you can see that you will have a XML file where you put the connection attributes: database URL, user, password, min and max pool connection size (how many connections to database will be open)

  • Tomcat way:

    <Resource name="jdbc/ProjectX" auth="Container"
        type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/projectx"
        username="user" password="password" maxActive="20" maxIdle="10" maxWait="-1"/>
    
  • JBoss way:

    <datasource jndi-name="jdbc/ProjectX" pool-name="MySqlDS">
            <connection-url>jdbc:mysql://localhost:3306/projectx</connection-url>
            <driver>com.mysql</driver>
            <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
            <pool>
                <min-pool-size>10</min-pool-size>
                <max-pool-size>100</max-pool-size>
                <prefill>true</prefill>
            </pool>
            <security>
                <user-name>user</user-name>
                <password>password</password>
            </security>
            <statement>
                <prepared-statement-cache-size>32</prepared-statement-cache-size>
                <share-prepared-statements/>
            </statement>
    </datasource>
    <drivers>
        <driver name="com.mysql" module="com.mysql">
            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
        </driver>
    </drivers>
    

In the end, if you have configured your database connection pool and it works, all you have to do in your code to recover the connection is call InitialContext#lookup to recover the resource using its JNDI resource name.

Knowing this, after configuring a JNDI resource to connect to MySQL database with name "jdbc/ProjectX", you can have a class which recovers the Connection like this:

public class DatabaseConnectivity {
    public static Connection getConnection() throws NamingException, SQLException {
        InitialContext cxt = new InitialContext();
        DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/ProjectX" );
        return ds.getConnection();
    }
}

By the way, I would use different names for packages to know the group of functionality of the classes. For example:

src
- edu.home.controller.servlet
  + CoreServlet.java
- edu.home.controller.filter
  + SessionFilter.java
- edu.home.model.entity
  + AnEntity.java
  + AnotherEntity.java
- edu.home.model.database
  + DatabaseConnectivity.java
- edu.home.model.service
  + AnEntityService.java
  + AnotherEntityService.java
(and on and on...)
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I got your point, but how can I interact b/w to classes, for example If I want to use CoreServlet.java variable in DatabaseConeectivity.java than how can we do that? – Anshul Apr 12 '13 at 22:14
  • 1
    @TechDon that's a complete different question. My answer covers the what's asked in the title of your question. The other questions are result of reading a tutorial. You can start learning from [StackOverflow Servlets wiki](http://stackoverflow.com/tags/servlets/info) and [StackOverflow JSP wiki](http://stackoverflow.com/tags/jsp/info) – Luiggi Mendoza Apr 12 '13 at 22:17