Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.polsl.java.lab6.model;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import javax.servlet.ServletContext;
- /**
- * Enable creating single database con during using of application.
- *
- * @author rdule
- * @version 1.0
- */
- public class DatabaseConnection {
- private static Connection con = null;
- /**
- * Method creates single database con
- *
- * @param context Gets con params.
- * @return Connection with database.
- * @throws SQLException Causes by SQL issues.
- * @throws ClassNotFoundException Driver not found.
- */
- public static synchronized Connection connectWithDatabase(ServletContext context) throws SQLException, ClassNotFoundException {
- if (con == null) {
- String driverPath = context.getInitParameter("driverUrl");
- Class.forName(driverPath);
- String databaseURL = context.getInitParameter("databaseURL");
- String username = context.getInitParameter("username");
- String password = context.getInitParameter("password");
- con = DriverManager.getConnection(databaseURL, username, password);
- }
- return con;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement