Home > Java, Oracle > java oracle connection

java oracle connection

Tomorrow i was trying to connect my java program with oracle server.It was not easy for me to do this.Since i’m a new learner.I google it about half an hour.I read every page that seems to me helpful.Then i finally got the solution.If i describe you,you will be astonished and will say to yourself it is nothing at all.Then let go…

Check your configuration with mine:
OS: Windows Xp
JDK Version: JDK1.6
IDE: Any ide or compiler will work
Oracle:10g

open ur ide or compiler. now create a new project ‘database’.When you will give ur project name meantime you have to notice that anywhere there is a option that have classpath or not.If there is open your classpath settings.Now add a new class.Locate this class to

…oracle/product/10.1.0/db_2/jdbc/lib/ojdbc14.jar

If you you don’t have ojdbc14.jar then ojdbc[].jar can work for you.Try it.Or you can download it.From oracle website.

ok,now we locate a class that can connect our jdk with oracle.

If you know in which port your oracle server is active then ok,if not
open you command prompt in your windows
now type lsnrctl status
what have you seen.From results you will find

Listening Endpoints Summary…
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC0ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=myhostname)(PORT=1521)))

now we need the name of SID
Now open your sql+ window from oracle
type
show parameter db_name

you will get a value [for me it was oracl]

this.So PORT=1521 is your port no.
Now,In your project creat a new file named ‘database.java’


import java.sql.*;

public class database
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:oracle:oci:@localhost:1521:oracl", "username", "pasword");
}

}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());

Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

System.out.println("Connection Successful");
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
};

And that’s it.And it is definitely worked for me.Hope,will work for you too.Have a nice programming.

Advertisements
Categories: Java, Oracle
  1. No comments yet.
  1. November 8, 2009 at 7:53 pm

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: