Monday 24 December 2012

database connection with oracle thin driver in java


Hi friends today m vikram parihar going to tell you that how we can connect our oracle database with java program using oracle thin driver (class 12)....

"  if you are using netbeans  then you should copy or set the libraries with your class driver(oracle thin driver)
like this..

select the new project and add the libraries using project property.
Add jar/folder..


then ok and apply..

now set the ODBC services

steps for set the ODBC services
1.  Start -> Control Panel -> Administrative tools.
2. Data Sources (ODBC) -> now select the System DSN  like this..




  1. now we add the DSN  "Microsoft ODBC for Oracle" and  finish.


but first you must install Oracle database express or Oracle database enterprise edition.


now type the following code for fetch record form table.. (student table)

import java.sql.*;
import util.*;
class student
{
public static void main(String agrs[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
    }
        catch(ClassNotFoundException e)
                {
            system.out.println("not registered");
        }
try
{
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");

// here 127.0.0.1 is my local host IP address and 1521 is oracle database port number,  XE is my oracle //database and system is my username and vicky is my database password.

Statement st = con.createStatement();
 ResultSet rs = st.executeQuery("select name, fname, roll_no, class from student");
while(rs.next())
{
system.out.print(rs.getString(1));
system.out.print(rs.getString(2));
system.out.print(rs.getString(3));
system.out.print(rs.getString(4));
}

}
 catch(SQLException e)
            {
               system.out.println("not connected to database"+e);
            }
}
}

this code will work 100 % if not please connect to me..
thanks...
please give me feedback..  










No comments:

Post a Comment