Tuesday, March 16, 2010

Java DataBase Connectivity JDBC

JDBC Basics

Types of JDBC Drivers
Type 1:- Bridge drivers. Example JDBC-ODBC Bridge
Type 2:- Native API drivers, partially in Java.
Type 3:-Middleware is involved in communication from client's request and data source.
Type 4:-The client connects directly to DataSource, pure Java drivers.

Establishing the Connection
Two ways:-
1) DriverManager class
2) DataSource interface

Using DriverManager class for making connection
Load the driver:-   Class.forName("oracle.jdbc.driver.OracleDriver");
Define Connection URL:-  String url = "jdbc:oracle:thin:@" + hostName + ":" + portName + ":" + dbName;
Establish Connection:- Connection conn =DriverManager.getConnection(url,username,password);

Using DataSource interface for making connection
InitialContext initContext = new InitialContext();
DataSource ds = initContext.lookup("java:comp/env/jdbc/ownDataBase");
Connection con = ds.getConnection();

The above method uses JNDI lookup to get the datasource.

Using Datasource for getting JDBC connection is preferred over DriverManager class.
 

Saturday, March 6, 2010

Creating a database in Oracle using dmp file

Hi,

Today I will pick up a very interesting, simple and yet important job of Software development, and that is creation of a database.

There are many databases available in the market. I prefer Oracle. Today I will show how to create a database in Oracle using a .dmp file.

I am working with Oracle 10g right now, so I will use it for reference.

If you do not have Oracle on your machine, just go to the Oracle download center, and download the required version from there. And the best part is that the basic version is free, and works for most of the applications we work for.

So, after copying the exe file from Oracle, and installing it on your machine, now we need to create a Database instance from the .dmp file.

For that follow the steps below:-
  1. Log in the Oracle Database home page with "SYSTEM" id and password(set during Oracle installation) . The url may vary from version to version and user to user. In my case it is:- http://127.0.0.1:8080/apex/f?p=4550:11:2121390824271097::NO:::
  2. Create a user in the Oracle Database using the Oracle database home screen.
  3. Grant the user all the rights and make him SYSDBA, so that he can import the .dmp file.
  4.  Now open the dos prompt by typing “cmd” in the “Run” dialogue box, or by clicking on it’s shortcut.
  5.  Type “IMP” on the dos prompt. Your screen will be something like shown below:-



Enter the Username and password for the User which you have created in the Oracle.

For my case, it is shown below:-

Now it is asking for the path of the .dmp file, just copy the path followed by the .dmp file name and press enter. You will see something like below:-



After pressing enter it will ask you to enter many other parameters, you may choose to use my configuration. The image below displays the configuration which I choose.



This is all you need to do, the import should terminate successfully, there might be some warnings, but usually they are not important.

In my coming articles, I will demonstrate how to use SQL Developer to interact with this database.

That's all for now. See you later.

Cheers....
Amit