Install Sun JDK on Linux

I’ve tested this procedure on Fedora 10 and Ubuntu 9.04 but it should work on other distributions too.

First of all download the latest JDK package from the SUN page: be sure to download the bin file and not the rpm.

From now on you’ll need to run commands using sudo.

Move the package to /opt/ and make it executable.

cd /path/to/download/folder

mv jdk-versionnumber-linux-i586.bin /opt/

cd /opt/

chmod +x jdk-versionnumber-linux-i586.bin

Now start the installation and follow the onscreen instructions

./jdk-versionnumber-linux-i586.bin

When the installation is done you’ll need to set the JAVA_HOME enviroment variable and add java executable to the system pah.

Open the file /etc/profile and add the following lines

JAVA_HOME="/opt/jdk_versionnumber"

export JAVA_HOME

PATH=$PATH:/opt/jdk_versionnumber/bin/

export PATH

Now add a symbolic link for the java command

ln -s /opt/jdk_versionnumber/bin/java /usr/bin/java

Log off and log in back and you’re done.

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 = "mysql.server.name";
    static final String dbport   = "mysql.database.port";
    static final String dbname   = "mysql.database.name";
    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 {

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

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    public static void close(Object object){

        if (object != null){

            try {

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

This is also a gist. Check it out