Pages

Sunday, August 22, 2010

How to check if an integer is a power of 2 ?

 

Source: http://www.codechef.com/wiki/tutorial-bitwise-operations

Explanation:

The Integer which is the power of two will always have the leftmost bit 1 and the remaining bits as 0. Let this number be x.

And the number (x-1) will never have the leftmost bit as 1. So x & (x-1) will always give 0 if the number is power of 2.

So to check whether the number is power of two, check x & (x-1) , if the answer is 0 then the number is infact power of 2.

Monday, August 9, 2010

JSP Servlets, Tomcat v6 and MySql

Installing the Server, Tomcat

  1. Install Java. Make sure you have Java 5 or 6 installed.
  2. Unzip Tomcat. Unzip tomcat-6.0.28-preconfigured.zip.
  3. Download Eclipse. Install the Java EE version of Eclipse. If you have JDK 1.6.0_21 or later, you must fix the Eclipse permgen space error.
  4. Tell Eclipse about Tomcat. Start Eclipse and go to the Workbench. Click on Servers tab at bottom. R-click, New, Server, Apache, Tomcat v6.0, navigate to Tomcat installation folder (e.g.,C:\apache-tomcat-6.0.28), OK.
  5. Test the server. Test Tomcat within Eclipse. Details here.
  6. Adjust Eclipse preferences. Suppress unnecessary warnings about serialized classes. Window, Preferences, Java, Compiler, Errors/Warnings, change "Serializable class without ..." to "Ignore". Many other personal preferences re font size, indentation style, etc.

Installing MySql Community Server (Database)

  • Use the following link http://dev.mysql.com/downloads/mysql/ to download MySql Community Server
  • MySQL will be installed as a service which will start up with Windows. It is recommended you accept most of the defaults although I did check in the "Include Bin Directory in Windows Path" checkbox (which allows Windows to find executables from the command line if you choose to run it that way occasionally) and you should add a password when given the opportunity. You should configure your firewall to accept the port(usually port 3306).
  • Download the Java MySql Connector dev.mysql.com/downloads/connector/j/5.1.html
  • Download and unzip the file linked above. Copy the mysql-connector-java-5.1.6-bin.jar to the WEB-INF/lib
  • Download the proper Graphic User Interface for your MySql: dev.mysql.com/downloads/gui-tools/5.0.html

Java Code to Create your Database, Make sure you replace username and password with the proper values:

import java.io.*;
import java.sql.*;

public class CreateDatabase{
public static void main(String[] args) {
System.out.println("Database creation example!");
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/","root","HashMap");
try{
Statement st = con.createStatement();
BufferedReader bf = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter Database name:");
String database = bf.readLine();
st.executeUpdate("CREATE DATABASE "+database);
System.out.println("1 row(s) affected");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}



Java Code to connect to the database you just created.



import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "Replace with YOUR SCHEMA NAME";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}