Java is a revolutionary language which was invented by James Gosling at Sun Microsystems. The most endearing thing about Java is its platform independence - which means you can literally compile it on one platform and then run it on any other platform be it Sparc, Intel, Motorola or PowerPC architecture running any of the OSes (Windows, OSX, Linux...). Recently I had the opportunity to work in Java/J2EE technology. And naturally I wanted to do programming in Linux instead of windows (basically because I am more productive and comfortable working in Linux than windows). Here I will detail the steps I took to install and configure Sun's JDK 1.5.0 on Linux.
- I Download the latest JDK (Java Development Kit - latest is ver 1.5.0) for Linux from Sun's java site. It was a zip file which, when I unpacked into my home directory, created a directory named 'jdk1.5.0'.
- Then I logged in as 'root' and moved the just created 'jdk1.5.0' directory into the /opt directory.
- Now again logging in as normal user, I appended the following lines to my .bashrc file.
#FILE .bashrc
...
JAVA_HOME=/opt/jdk1.5.0
export JAVA_HOME
export PATH=$PATH:/opt/jdk1.5.0/bin
CLASSPATH=/opt/jdk1.5.0/lib/tools.jar: \ /opt/jdk1.5.0/lib/dt.jar
export CLASSPATH
...
As you can see in the above listing, I have set the JAVA_HOME variable to point to my Java installation directory. This is needed because some application servers like JBoss will be checking if Java is installed on your system using JAVA_HOME variable. And I have also modified the PATH variable to point to the Java bin directory. I have set a third variable CLASSPATH which points to the libraries needed to compile Java programs. This is needed because when you compile your Java programs in the command line, the compiler needs to know where the libraries are located. This it achieves by reading the CLASSPATH variable. Don't forget to export JAVA_HOME, PATH and CLASSPATH. Once you have edited your .bashrc file, open a terminal and execute the command to make the bash shell to re-read the contents of the .bashrc file as follows:
$ source ~/.bashrc
Now you can compile and execute Java programs in Linux just like you do in windows.
MySQL integration in your Java Programs
Java has excellent support and integration with all major databases through the JDBC API. Since MySQL - a time tested robust database - comes bundled by default with most Linux distributions, any Java programmer on the Linux platform need not look at alternative databases. But to program in Java using JDBC to connect to MySQL database, you have to download and install the MySQL-Java driver. Below I list the steps needed to configure Java to work with MySQL.
- First go to the MySQL website and download the MySQL ConnectorJ driver for Java file (Size: 6.6 MB). When I downloaded, the latest version was 3.1.10. (mysql-connector-java-3.1.10.tgz ), which might have changed by now. This is a gzipped tape archive. So unpack it into a directory of your choice as follows:
$ tar -xvzf mysql-connector-java-3.1.10.tgz
This will unpack it into a new directory by name mysql-connector-java-3.1.10. - Now login as root and move the whole directory into the /opt directory.
- Next logout as root and login as normal user and modify CLASSPATH in your .bashrc file as follows:
# FILE .bashrc
...
CLASSPATH=/opt/jdk1.5.0/lib/tools.jar:/opt/jdk1.5.0/lib/dt.jar \\ :/opt/mysql-connector-java-3.1.10/ \ mysql-connector-java-3.1.10-bin.jar
export CLASSPATH
...
That is it. Now you can write JDBC code in Java to connect to MySQL just like you do it in windows or any other platform. Below I have written the section where you need to enter MySQL specific code :
# Program Snippet
...
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "jdbc:mysql://localhost:3306/mydatabasename";
Connection con = DriverManager.getConnection(url,"username","password");
// The rest of the code is common for all databases.
}
...
No comments:
Post a Comment