Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, May 18, 2012

No JDK found - please set JAVA_HOME error while starting Apache tomcat in ubuntu

If you have already installed JDK and JRE in your system and encountered this error, then follow below steps.

1) Type sudo gedit ~/.bashrc in the terminal
2) Add export JAVA_HOME=/usr/lib/jvm/java-7-oracle  at the end of the file.  Here the path may vary depending upon your installation.  Open /usr/lib/jvm and check for the folder name and replace accordingly.  It is java-7-oracle in my case. This is nothing but setting up environmental path for java.


Check out if your Apache tomcat is starting.  If the same error encounters again, goto next step

3) Type sudo gedit /etc/default/tomcat6  in the terminal and hit enter.
( Type password if asked ).
4) Search for the word something similar to this JAVA_HOME=/usr/lib/jvm/java-7-oracle
If this line is commented, uncomment it and give the same path which you have given previously in .bashrc file.
5) Save the file and try to start the server again. It works!



If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

Thursday, May 17, 2012

Which one is better ? open-jre or sun java ?

On a whole, open java and sun java are 99 % similar. As oracle purchased sun company, java is taken into oracle and tried to make it proprietary. Open jdk / open jre is under GNU Public License and is free (free in terms of freedom not as in price). So whatever may be the issue, there are currently no major issues running both of them side by side. 


But today, as I re-installed my Operating system i.e Ubuntu, I need to check for java plugin support for browser. I've already installed open-jre in my system but it don't enable jre in browser i.e java plugin in browser. i didn't find any browser specific plugins on the internet. Tried installing sun java from oracle.com but couldn't succeed because I've downloaded a Linux version .tar.gz file which is self extracting archive. Oracle.com suggested to execute .bin file in that folder and surprisingly it is missing (Amazing support !).


Finally discovered a way on the internet and below are the steps.


sudo add-apt-repository ppa:webupd8team/java


sudo apt-get update


sudo apt-get install oracle-java7-installer


sudo apt-get update

Voila ! that works ! :)

Consider this before choosing.



If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

Friday, May 20, 2011

JDBC

This article deals with JDBC completely.

What is JDBC?
Posted on: April 13, 2007 at 12:00 AM
JDBC is Java application programming interface that allows the Java programmers to access database management system from Java code.

The Java application programming interface provides a mechanism for dynamically loading the correct Java packages and drivers and registering them with the JDBC Driver Manager that is used as a connection factory for creating JDBC connections which supports creating and executing statements such as SQL INSERT, UPDATE and DELETE. Driver Manager is the backbone of the jdbc architecture.

JDBC has four Components:

1. The JDBC API.
2. The JDBC Driver Manager.
3. The JDBC Test Suite.
4. The JDBC-ODBC Bridge.


The API technology provides the industrial standard for independently connecting Java programming language and a wide range of databases.



The JDBC Driver Manager.

The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. Usually  Driver Manager is the backbone of the JDBC architecture. It's very simple and small that  is used to provide a means of managing the different types of JDBC database driver running on an application. The main responsibility of  JDBC database driver is to load all the drivers found in the system properly as well as to select the most  appropriate driver from opening a connection to a database.  The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected.

The JDBC Test Suite.

The function of  JDBC driver test suite is to make ensure that the  JDBC drivers will run user's program or not .

 The JDBC-ODBC Bridge.

The JDBC-ODBC bridge, also known as JDBC type 1 driver is a  database driver that utilize the ODBC driver to connect  the  database. This driver translates JDBC method calls into ODBC function calls. The Bridge implements Jdbc for any database for which an Odbc driver is available. The Bridge is always implemented as the sun.jdbc.odbc Java package and it contains a native library used to access ODBC.


Layers of the JDBC Architecture




Type 1 JDBC Architecture



Type 2 JDBC Architecture




Accessing Database using Java and JDBC


For this, firstly we need to establish a connection between database and java file with the help of various types of APIs, interfaces and methods. We are using MySQL database.
Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.
Class.forName(String driver): It loads the driver.
DriverManager: This class controls a set of JDBC drivers. Each driver has to be register with this class.
getConnection(String url, String userName, String password): This method establishes a connection to specified database url. It is having three arguments:
        url: - Database url where stored or created your database
        username: - User name of MySQL
        password: -Password of MySQL
getMetaData(): This is a method of Connection interface. It retrieves the metadata of the database.
DataBaseMetaData: This interface gives information about the database like number of tables in the database, columns of the table etc.
getTables(null, null, "%", null): This method provides the description of the tables available in the given catalog. As we have set other parameters null, so it will provide only table names.

Code :

import java.sql.*;

public class AccessDatabases {
  public static void main(String[] args) {
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection con = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test""root""root");
      Statement st = con.createStatement();
      DatabaseMetaData meta = con.getMetaData();
      ResultSet rs = meta.getTables(null, null, "%"null);
      String tableNames = "";
      while (rs.next()) {
        tableNames = rs.getString(3);
        System.out.println(tableNames);
      }
    catch (Exception e) {
    }
  }
}





In this section we studies how to connect to database and then list all the tables in the database. We have used MySQL database server.




FOR REST OF THE TOPICS LIKE EXCEPTION HANDLING AND ETC,. GOTO SOURCE
Source : RoseIndia

Wednesday, May 18, 2011

Difference between Pointer and reference

In OOP like Java, guys often speak that there are no pointers but there are references. Here the reference or a reference pointers like as follows.
Dog d;
d.setName();

Here d is reference.
Actual concept of pointers is not implemented in Java due to the reasons like confusion and the hell caused by them.

However there are pointers in Java but in JCuda (Java bindings for the CUDA runtime and driver API) http://www.jcuda.de/jcuda/doc/index.html there is a class called jcuda.Pointer.                      


Java automatic memory management from Wikipedia

" One of the ideas behind Java's automatic memory management model is that programmers can be spared the burden of having to perform manual memory management. In some languages, memory for the creation of objects is implicitly allocated on the stack, or explicitly allocated and deallocated from the heap. In the latter case the responsibility of managing memory resides with the programmer. If the program does not deallocate an object, a memory leak occurs. If the program attempts to access or deallocate memory that has already been deallocated, the result is undefined and difficult to predict, and the program is likely to become unstable and/or crash. This can be partially remedied by the use of smart pointers, but these add overhead and complexity. Note that garbage collection does not prevent "logical" memory leaks, i.e. those where the memory is still referenced but never used. "