Home > JDBC, Technical > JDBC Oracle Connection Example

JDBC Oracle Connection Example

February 22, 2012

Simple java program to connect to Oracle server

Note: Before running the program,Place the Oracle jars in class path

In My case:\jdbc\lib\ojdbc5.jar,\jdbc\lib\classes15.jar

import java.sql.*;
public class test {
public static void main(String args[]) throws Exception
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String str=null;
try
{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
String serverName = “your server ip”;
String portNumber = “1521”;
String sid = “your sid”;
String url = “jdbc:oracle:thin:@” + serverName + “:” + portNumber + “:” + sid;
System.out.println(url);
String username = “karuvachi”;
String password = “iamhere”;
con = DriverManager.getConnection(url, username, password);
System.out.println(“Connection successful”);
stmt=con.createStatement();
rs=stmt.executeQuery(“select sysdate from dual”);
while(rs.next())
{
str=rs.getString(1);
System.out.println(str);
}

}
catch(Exception ex)
{
System.out.println(ex);

}

finally{
con.close();
}
}
}

Advertisements
Categories: JDBC, Technical
%d bloggers like this: