Setting up a JDBC connection to Oracle 11g from java class

For setting up a JDBC connection , we need to have the following data primarily:

DB_URL = “jdbc:oracle:thin:@localhost:1521:xe”;

This is for setting up connection to oracle 11g. For any other databases the DB_URL and the DRIVER may vary. Also  the URL pattern depends on the port and the database that we are trying to connect to and the details may be obtained in tnsnames.ora file in  <drive:\>oracleexe\app\oracle\product\11.2.0\server\network\ADMIN path

XE =
(ADDRESS = (PROTOCOL = TCP)(HOST = Devashish-PC)(PORT = 1521))

Now comes the database username and password. If you already have a user created then you can skip this step else do the following:

1. In start menu in the oracle11g folder click on get started.
2. When prompted to enter the username type ‘system’ and password type ‘tiger’.
3. Localhost appears.
4. Click on application express.
5. Create the database user.

Suppose my user is created with credentials :

USER = “dev”;
PASS = “june”;

Now when the following steps has been performed. Go to eclipse and create a new project. Download ojdbc14 , configure buildpath and add as external jar.

Then the the code for connecting is :

final String JDBC_DRIVER="oracle.jdbc.driver.OracleDriver";
final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
// Database credentials
final String USER = "dev";
final String PASS = "june";
Connection con=null;
Statement stmt=null;
try {
ResultSet rs=stmt.executeQuery("select * from tab");
} catch (Exception e) {

It should return the no of tables fetched e.g. in my case it showed 10.

Happy coding  🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s