Posts Tagged MySQL

Setup a LAMP server on Ubuntu

These are the steps to install an Apache-MySQL-PHP server on an Ubuntu desktop machine.

# Install Apache 2
sudo apt-get install apache2
 
#Install PHP5
sudo apt-get install php5
 
#Install MySQL
sudo apt-get install mysql-server
 
#Install MySQL for Apache
sudo apt-get install libapache2-mod-auth-mysql
 
#Install PHP MySQL Support
sudo apt-get install libapache2-mod-php5
sudo apt-get install php5-mysql
 
#Restart Apache
sudo /etc/init.d/apache2 restart

Managing Apache

Web files must be placed in ”/var/www/”.

sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 restart

Managing MySql

sudo /etc/init.d/mysqld start
sudo /etc/init.d/mysqld stop
sudo /etc/init.d/mysqld restart

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;
            }
        }
    }
}