Sample jdbc program using oracle database

Hi everyone, in this post, I will be writing about a sample jdbc program using oracle database. I hope everyone have gone through my earlier post, sample program using HSQL DB. Connection of oracle database is almost similar to connection with hsql db, except the name of connection url and class for connection.

Connection URL = "jdbc:oracle:thin:@localhost:1521"
Class.forName("oracle.jdbc.driver.OracleDriver")

Here, I am writing a jdbc program to fetch the values from a table. The sample jdbc program for oracle database is as follows :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class OracleDatabase {

    public static void main(String[] args) {
        
        System.out.println(" Oracle Database ");
        System.out.println();
        try
        {
            String connectionURL ="jdbc:oracle:thin:@localhost:1521";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            Connection con = DriverManager.getConnection(connectionURL,"SYSTEM","password");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(" select * from aa_employee");
            while(rs.next())
            {
                System.out.println("Id = "+rs.getString(1));
                System.out.println("Name = "+rs.getString(2));
                System.out.println("State = "+rs.getString(3));
                System.out.println();
            }    
            
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            
        }
    }
}


The default user name of the oracle database is "SYSTEM" and password is the password which you provide during installing the oracle database.

The jar file for ojdbc can be downloaded from here.

The entries of the database is as follows:


Lets see what is the output which we got after running the program:

 Oracle Database 

Id = 1
Name = APPU
State = TEXAS

Id = 2
Name = COOKIE
State = MASSACHUSSETTS


Thus we got all the details from the table through our program.

I hope everyone understood this program very well. Please comment below if you have any doubts. All comments will be responded soon.

Thank you very much for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++