Get the WEB-INF folder path

This is a simple example on how to get the WEB-INF directory path in a J2EE web application.

public class MyClassName {

  private static final String WEBINF = "WEB-INF";

  public String getWebInfPath(){

    String filePath = "";

    URL url = MyClassName.class.getResource("MyClassName.class");

    String className = url.getFile();

    filePath = className.substring(0,className.indexOf(WEBINF) + WEBINF.length());

    return filePath;


Connect to MySql using Java

This is a simple class that I usually use to connect to a MySQL database. Obviously the right MySQL JDBC driver must be in your classpath.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionManager {

    // Connection data -- START

    static final String driver   = "com.mysql.jdbc.my_driver_version";
    static final String dbserver = "";
    static final String dbport   = "mysql.database.port";
    static final String dbname   = "";
    static final String dbuser   = "mysql.database.username";
    static final String dbpass   = "mysql.database.password";

    // Connection data -- END

     * Opens a connection to the database
     * @return Returns a Connection object

    public static Connection getConnection(){

        Connection con = null;

        try {

            String conString = "jdbc:mysql://"+dbserver+":"+dbport+"/"+dbname;
            con = DriverManager.getConnection(conString,dbuser, dbpass);

        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (ClassNotFoundException e) {
        } catch (SQLException e) {

        return con;

    public static void close(Object object){

        if (object != null){

            try {

                if (object instanceof Connection)
                else if (object instanceof Statement)
                else if (object instanceof PreparedStatement)
                else if (object instanceof ResultSet)
            } catch (SQLException e) {
            } finally {
                object = null;

This is also a gist. Check it out