skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, September 22, 2012

Sample code to access Oracle DB

Posted by Admin at 4:53 AM – 0 comments
 

Using this sample code you can access the Oracle DB. This can be very easily modified to suit your own project/application.


import java.io.*;
import java.util.*;
import java.sql.DriverManager;
import java.sql.* ;

public class ReadFromDb
{
 public static Connection gConnection;
 public static Connection uConnection;

 public static void main(String args[])
    {
        // Driver Name or Type
        // URL Name
        // DB userid Name
        // DB password Name
        String cDriverName = "oracle.jdbc.driver.OracleDriver";
  String url = "jdbc:oracle:thin:oplus_app/oplus_app@obsdevdb.qintra.com:1528:uordps00";
  String userid = "oplus_app";
  String password = "oplus_app";

  try
  {
   Class.forName(cDriverName);
      gConnection = DriverManager.getConnection(url, userid, password);
  }
        catch(Exception e)
  {
   System.err.print("Exception1: ");
   System.err.println(e.getMessage());
  }
  try
  {
   Class.forName(cDriverName);
      uConnection = DriverManager.getConnection(url, userid, password);
  }
        catch(Exception e)
  {
   System.err.print("Exception2: ");
   System.err.println(e.getMessage());
  }

        ReadFromDb objGet = new ReadFromDb();
        try
        {
   objGet.getOrder();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

   // Method where the implementation is there.
   void getOrder() throws Exception
 {
  ResultSet sResult = null;
  String order_number = "C 11820407";
  PreparedStatement ps = null;

  try
  {
   Statement sStatement = gConnection.createStatement();

   // Query. Every project can use their own.
   String sQuery1 = "SELECT ORDER_CONTEXT_ID,DESTINATION_SYSTEM,ORDER_STATUS,IS_ORDER_VALID,CUID,XML_ORDER FROM ORDER_PDS_XML "
                                + " WHERE ORDER_NUMBER = '" + order_number + "'" ;

   System.out.println("\n"+sQuery1);

            sResult = sStatement.executeQuery(sQuery1);

   if (sResult != null)
   {
    while(sResult.next())
    {
     String oci = sResult.getString("ORDER_CONTEXT_ID");
     System.out.println("\nORDER_CONTEXT_ID : " + oci);

     String sQuery2 = "UPDATE ORDER_PDS_XML SET REGION = 'NWB', MARKET_SEGMENT=1"
                                     + " WHERE ORDER_CONTEXT_ID = '" + oci + "'" ;

     System.out.println("\n"+sQuery2);
     ps = uConnection.prepareStatement(sQuery2);
     ps.executeUpdate();
    }
   }

   if(sStatement != null)
   {
    sStatement.close();
    sStatement = null;
   }
   if(sResult != null)
   {
    sResult.close();
    sResult = null;
   }
   if (ps != null)
   {
    ps.close();
   }
   if (uConnection != null)
   {
    uConnection.close();
   }
   if (gConnection != null)
   {
    gConnection.close();
   }
  }
  catch (SQLException ex)
  {
   ex.printStackTrace();
  }

 }

}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger