Results 1 to 5 of 5
  1. #1
    silia_motor is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default oracle JDBC java

    Hi,

    I have a new Vista, my old XP died. I am having difficulty executing a simple JDBC/java progam to access oracle db. When I worked before, I was using
    9g. Now it is 10g. I can not figure out if thin client is to be used or OCI. I used both of them before. Where to place the classes12.jar or another one that goes with oci driver. And how to do classpath? Your help will be greatly appreciated. Thanks

    My code is as follows:

    import java.sql.*; // JDBC package
    //import com.inet.tds.JDBCRowSet;
    import java.util.*;
    // import oracle.jdbc.driver.*;
    // import oracle.sql.*;


    public class HomeDB
    {
    /*
    run as follows:
    C:\Documents and Settings\silia>cd \jj
    C:\john>set CLASSPATH=.;c:\oracle9i\classes12;%CLASSPATH%
    C:\john>javac HomeDB.java
    C:\john>java HomeDB

    King: 24000.0
    Kochhar: 17000.0
    De Haan: 17000.0
    Hunold: 9000.0
    Ernst: 6000.0
    Austin: 4800.0
    */


    public static void main(String[] argv)throws SQLException
    {
    Connection conn=null;
    try // may throw a SQLException
    {
    conn = getConnection();
    doQuery (conn);
    }
    catch (SQLException e)
    {
    System.err.println(e.getErrorCode() + ": " + e.getMessage());
    }
    finally // make sure the connection is closed
    {
    if (conn != null) try {conn.close();} catch (SQLException e) {};
    }
    //out.close(); // close PrintWriter stream
    }
    private static Connection getConnection() throws SQLException
    {

    String username = "scott";
    String password = "tiger";
    String url = "jdbc:oracle:thin:@localhost:1521:newora";
    // "jdbc:oracle:thin:@localhost:1521:COCKYJOB";

    Connection conn = null;
    String driver = "oracle.jdbc.driver.OracleDriver";

    try {
    Class.forName("oracle.jdbc.driver.OracleDriver").n ewInstance();
    } catch ( ClassNotFoundException cnfex ) {
    System.err.println(
    "Failed to load JDBC/ODBC driver." );
    cnfex.printStackTrace();
    }catch (Exception e){
    e.printStackTrace();
    }

    //DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

    conn = java.sql.DriverManager.getConnection(url,username, password);
    conn.setAutoCommit(false);
    return conn;
    }
    private static void doQuery (Connection c) throws SQLException
    {
    // statement to be executed
    String query = "SELECT ename, job, mgr FROM EMP";
    Statement st = null;
    ResultSet rs = null;
    try // make sure the close statements are executed (in the finally block)
    {
    // create the statement
    st = c.createStatement();
    // execute the query
    rs = st.executeQuery(query);
    // process results
    while (rs.next())
    {
    // get the employee last name
    String eName = rs.getString("ename");
    String eJob = rs.getString("job");
    String eMgr = rs.getString("mgr");
    System.out.println("Emp Name:" + eName +
    "Job: " + eJob +
    "MGR: " + eMgr);
    }
    }
    finally // make sure the close statements are executed
    {
    // close the result set if it exists
    if (rs != null) try {rs.close();} catch (Exception e) {};
    // close the statement if it exists
    if (st != null) try {st.close();} catch (Exception e) {};
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by silia_motor View Post
    Hi,

    I have a new Vista, my old XP died. I am having difficulty executing a simple JDBC/java progam to access oracle db. When I worked before, I was using
    9g. Now it is 10g. I can not figure out if thin client is to be used or OCI. I used both of them before. Where to place the classes12.jar or another one that goes with oci driver. And how to do classpath? Your help will be greatly appreciated. Thanks

    My code is as follows:

    import java.sql.*; // JDBC package
    //import com.inet.tds.JDBCRowSet;
    import java.util.*;
    // import oracle.jdbc.driver.*;
    // import oracle.sql.*;


    public class HomeDB
    {
    /*
    run as follows:
    C:\Documents and Settings\silia>cd \jj
    C:\john>set CLASSPATH=.;c:\oracle9i\classes12;%CLASSPATH%
    C:\john>javac HomeDB.java
    C:\john>java HomeDB

    King: 24000.0
    Kochhar: 17000.0
    De Haan: 17000.0
    Hunold: 9000.0
    Ernst: 6000.0
    Austin: 4800.0
    */


    public static void main(String[] argv)throws SQLException
    {
    Connection conn=null;
    try // may throw a SQLException
    {
    conn = getConnection();
    doQuery (conn);
    }
    catch (SQLException e)
    {
    System.err.println(e.getErrorCode() + ": " + e.getMessage());
    }
    finally // make sure the connection is closed
    {
    if (conn != null) try {conn.close();} catch (SQLException e) {};
    }
    //out.close(); // close PrintWriter stream
    }
    private static Connection getConnection() throws SQLException
    {

    String username = "scott";
    String password = "tiger";
    String url = "jdbc:oracle:thin:@localhost:1521:newora";
    // "jdbc:oracle:thin:@localhost:1521:COCKYJOB";

    Connection conn = null;
    String driver = "oracle.jdbc.driver.OracleDriver";

    try {
    Class.forName("oracle.jdbc.driver.OracleDriver").n ewInstance();
    } catch ( ClassNotFoundException cnfex ) {
    System.err.println(
    "Failed to load JDBC/ODBC driver." );
    cnfex.printStackTrace();
    }catch (Exception e){
    e.printStackTrace();
    }

    //DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

    conn = java.sql.DriverManager.getConnection(url,username, password);
    conn.setAutoCommit(false);
    return conn;
    }
    private static void doQuery (Connection c) throws SQLException
    {
    // statement to be executed
    String query = "SELECT ename, job, mgr FROM EMP";
    Statement st = null;
    ResultSet rs = null;
    try // make sure the close statements are executed (in the finally block)
    {
    // create the statement
    st = c.createStatement();
    // execute the query
    rs = st.executeQuery(query);
    // process results
    while (rs.next())
    {
    // get the employee last name
    String eName = rs.getString("ename");
    String eJob = rs.getString("job");
    String eMgr = rs.getString("mgr");
    System.out.println("Emp Name:" + eName +
    "Job: " + eJob +
    "MGR: " + eMgr);
    }
    }
    finally // make sure the close statements are executed
    {
    // close the result set if it exists
    if (rs != null) try {rs.close();} catch (Exception e) {};
    // close the statement if it exists
    if (st != null) try {st.close();} catch (Exception e) {};
    }
    }
    }
    hi..

    use any ide and then add those jars to the project..its very simple
    y wasting so many time to set path/classpath???

  4. #4
    jaizsankar is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Smile

    hai thanks

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    jaizsankar... stop answering nonsense on old posts... you could get yourself banned. If you have a specific Java question/problem, please post it in the correct subforum.

    Thank you,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2010, 02:57 PM
  2. Replies: 0
    Last Post: 03-21-2008, 02:13 PM
  3. Using JDBC to connect to ORACLE database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 12:27 PM
  4. Jdbc Driver For Oracle
    By Swamipsn in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 05:31 AM
  5. Oracle and JDBC
    By Eric in forum JDBC
    Replies: 3
    Last Post: 08-11-2007, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •