Results 1 to 6 of 6

Thread: how to connect advanced java with oracle

  1. #1
    Join Date
    Dec 2010
    Posts
    79

    how to connect advanced java with oracle

    I want to make project using advanced java and oracle. I don’t have any experience of making project till yet. But my group member have a knowledge about advanced java. But we don’t know the connectivity. I had ask all of my friend, but they don’t know the connectivity. Please tell me how to connect advanced java with oracle. I’ll be very thankful to you. Please help me.

  2. #2
    Join Date
    May 2008
    Posts
    962

    Re: how to connect advanced java with oracle

    If you want to use oracle with jdbc you need to setpath first, these can be done by following steps:
    Step:1
    Setting CLASSPATH
    1. Right click the My Computer icon. In that Click properties
    2. You will see a window System
    Properties. In that window click Advanced
    3. You will see a button Environment Variables. Click that button.
    4. In the popup window you will see User variables for XXXXXX (your name) and System variable. Select the New button which is below the User variables for XXXXX.
    5. You will see the window New User Variable. In Variable name type CLASSPATH and in Variable value type .;E:\oracle\product\10.1.0\Db_1\jdbc\lib\ojdbc14.jar
    The first entry must be a period, which denotes the current directory. The second entry must be the directory for the ojdbc14.jar or classess12.zip (jdk 1.2 or below).
    If CLASSPATH is not set correctly, you will get a NoClassDefFoundError error when you run a compile class.
    Be sure to use a version of the JDK that is compatible with the Oracle release you are using. If you use a new release of the JDK with an older release of Oracle’s drivers
    , you may encounter “access violation” errors when executing your programs.

    Step 2
    The PATH environment variable should already be set for JDK

    Step 3
    JDK is already provided by Oracle itself(You can even use your own JDK). If you install Oracle10g It comes automatically. It lies in
    E:\oracle\product\10.1.0\Db_1\jdk(I have installed oracle in E: in your case it might be c: or d: ….)

    By applying above steps you will be able to set your path for connection.

  3. #3
    Join Date
    May 2008
    Posts
    991

    Re: how to connect advanced java with oracle

    For connection first you have to set a classpath. After setting classpath, check the connection and write a following code. These code will make your connection with oracle:
    1. /**
    2. * ConnectOracle.java
    3. */
    4. package com.javaworkspace.connectoracle;
    5.
    6. import java.sql.Connection;
    7. import java.sql.DriverManager;
    8. import java.sql.ResultSet;
    9. import java.sql.Statement;
    10.
    11. /**
    12. * @author www.javaworkspace.com
    13. *
    14. */
    15. public class ConnectOracle {
    16. public static void main(String[] args) {
    17. try {
    18. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    19. Connection connection = DriverManager.getConnection(
    20. "jdbc:oracle:thin:@localhost:1521:orcl", "scott", "t");
    21.
    22. Statement statement = connection.createStatement();
    23. ResultSet resultSet = statement
    24. .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
    25. while (resultSet.next()) {
    26. System.out.println("EMPLOYEE NAME:"
    27. + resultSet.getString("EMPNAME"));
    28. }
    29. } catch (Exception e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. }
    The above code will make your advanced java connection with database.

  4. #4
    Join Date
    May 2008
    Posts
    1,020

    Re: how to connect advanced java with oracle

    To make a connection with oracle you have to set the path first, after setting classpath
    You have to include a following code to make a connection.

    1. /**
    2. * ConnectOracle.java
    3. */
    4. package com.javaworkspace.connectoracle;
    5.
    6. import java.sql.Connection;
    7. import java.sql.DriverManager;
    8. import java.sql.ResultSet;
    9. import java.sql.Statement;
    10.
    11. /**
    12. * @author www.javaworkspace.com
    13. *
    14. */
    15. public class ConnectOracle {
    16. public static void main(String[] args) {
    17. try {
    18. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    19. Connection connection = DriverManager.getConnection(
    20. "jdbc:oracle:thin:@localhost:1521:orcl", "scott", "t");
    21.
    22. Statement statement = connection.createStatement();
    23. ResultSet resultSet = statement
    24. .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
    25. while (resultSet.next()) {
    26. System.out.println("EMPLOYEE NAME:"
    27. + resultSet.getString("EMPNAME"));
    28. }
    29. } catch (Exception e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. }

  5. #5
    Join Date
    Nov 2009
    Posts
    824

    Re: how to connect advanced java with oracle

    There are two types of driver in oracle we will use both types of driver. First, we use the thin driver to connect to Oracle and execute an update statement. Try to understand and implement the code.
    import java.io.*;
    import java.sql.*;

    public class OraThin {
    public static void main(String[] args) {
    try {
    Connection con=null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection(
    "jdbc:oracle:thin:@machine_name:1521:database_name",
    "scott",
    "tiger");
    Statement s=con.createStatement();
    s.execute("
    INSERT INTO BOOKS VALUES
    (
    'A Tale of Two Cities',
    'William Shakespeare',
    4567891231,
    '5-JAN-1962'
    )
    ");
    s.close();
    con.close();
    } catch(Exception e){e.printStackTrace();}
    }
    }
    • The package java.sql containing the JDBC classes is imported.
    • The class OracleDriver, in the package oracle.jdbc.driver is dynamically loaded into the Java runtime using Class.forName(...).
    • A connection is requested to the database corresponding to the connection URL with the statement DriverManager.getConnection(...).
    • scott/tiger is an Oracle user/password.
    • We use instances of the Connection and Statement classes to perform a database operation.
    • The resources hold by these are released using their close() method.
    The only element now required is the service name of the database: database_name. The remainder is resolved using the local Oracle configuration file.
    DriverManager.getConnection(
    "jdbc:oracle:oci8:@database_name", "scott","tiger");

    By running above code you can make the conection to the database.

  6. #6
    Join Date
    Nov 2009
    Posts
    758

    Re: how to connect advanced java with oracle

    To make a connection with a oracle you need to create a jdbc connection. you must first import the
    java.sql package.
    Loading a database driver:
    try {
    Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); //Or any other driver
    }
    catch(Exception x){
    System.out.println( “Unable to load the driver class!” );
    }

    Step 1). Creating a oracle jdbc Connection:
    DBC URL Syntax:: jdbc: <subprotocol>: <subname>

    DBC URL Example:: jdbc: <subprotocol>: <subname>•Each driver has its own subprotocol
    •Each subprotocol has its own syntax for the source. We’re using the jdbc odbc subprotocol, so the DriverManager knows to use the sun.jdbc.odbc.JdbcOdbcDriver.
    try{
    Connection dbConnection=DriverManager.getConnection(url,”loginName”,”Password”)
    }
    catch( SQLException x ){
    System.out.println( “Couldn’t get connection!” );
    }

    Step 2). Creating a jdbc Statement object:
    Once a connection is obtained we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.
    Statement statement = dbConnection.createStatement();

    Step 3). Executing a SQL statement with the Statement object, and returning a jdbc resultSet.
    Test JDBC Driver Installation
    import javax.swing.JOptionPane;

    public class TestJDBCDriverInstallation_Oracle {

    public static void main(String[] args) {
    StringBuffer output = new StringBuffer();
    output.append(”Testing oracle driver installation \n”);
    try {
    String className = “sun.jdbc.odbc.JdbcOdbcDriver”;
    Class driverObject = Class.forName(className);
    output.append(”Driver : “+driverObject+”\n”);
    output.append(”Driver Installation Successful”);
    JOptionPane.showMessageDialog(null, output);
    } catch (Exception e) {
    output = new StringBuffer();
    output.append(”Driver Installation FAILED\n”);
    JOptionPane.showMessageDialog(null, output);
    System.out.println(”Failed: Driver Error: ” + e.getMessage());
    }
    }
    }

Similar Threads

  1. How to connect advanced java with sql server 2000
    By Mewad in forum Software Development
    Replies: 4
    Last Post: 04-01-2011, 04:24 PM
  2. Advanced Cache Objects in Java
    By StudyBoy in forum Tips & Tweaks
    Replies: 3
    Last Post: 31-07-2010, 03:41 PM
  3. Java in oracle 9i database
    By Gerri in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 08:35 PM
  4. Comparison between core java and advanced java
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 10:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,565,319.53500 seconds with 17 queries