0

I am new to Java web project and am using Jsp and Servlet in it, in order to connect with the database my each and every jsp file is making a new JDBC connection and connecting through it.

Is there any other way I can have a common place for JDBC connection and through that I can communicate with Database?

Parttimereaper
  • 593
  • 5
  • 16
VijayIndia
  • 91
  • 1
  • 7
  • 2
    I'd look into database connection pooling if I were you. [Something like this](http://stackoverflow.com/questions/2835090/how-to-establish-a-connection-pool-in-jdbc) – Parttimereaper Jan 12 '16 at 19:37

1 Answers1

4

For simple non-production projects,

public class DatabaseConnection{
    private static Connection conn = new Connection(db conn properties);

    public static Connection getConnection(){
         return conn;
    }
}

Wherever you need database connections, use

DatabaseConnection.getConnection()

If you want things like stale condition check etc etc. go for a connection pool. As it would be faster and better to include a open source connection pool than implementing these features manually.

For production environments, you can use Connection Pools like c3p0, hikariCP

prem kumar
  • 5,641
  • 3
  • 24
  • 36