1.4 Getting Connected

What good is a SQL program without a database connection? Another important import line is the following.

    import oracle.sqlj.runtime.Oracle;

The first thing you must do before executing a SQLJ statement is to connect to the database. (Note: Not true in the server — the stored procedure or function that is implemented by a SQLJ method runs in a database session that already has a connection going for it!)

Oracle.connect("jdbc:oracle:oci8:@", "scott", "tiger");

Your user name —equivalent to the database schema you are connecting to— is "scott" and your password "tiger". The first argument to connect() is the JDBC URL. If you want to connect to a different database, just place the database alias from your $ORACLE_HOME/work/tnsnames.ora after the "@". The following are connect strings for Oracle JDBC.

    jdbc:oracle:oci7:@ For an OCI7 connection
    jdbc:oracle:oci8:@ For an OCI8 connection
    jdbc:oracle:thin:@hostname:port:oracle-sid For a thin JDBC connection
    jdbc:oracle:kprb For the session in the server.

      Table 1 - List of Oracle JDBC URLs.

      The following is a sample thin JDBC URL: "jdbc:oracle:thin:@localhost:1521:orcl".

      The connect() method also has a twin: Oracle.close() — always invoke this method to close your connection!

      • Refer to the JDBC documentation for specifics — JDBC Developer's Guide and Reference, Chapter 3, "First Steps in JDBC".
      • There are many more ways to establish a connection in SQLJ than the particular Oracle.connect() method shown here. For more information, refer to SQLJ Developer's Guide and Reference, Chapter 4, Section "Connection Considerations" and —for advanced users— Chapter 7, Section "Connection Contexts".
      • The Oracle.connect() method sets the single, static connection for your program. If your program uses multiple connections, or you program an applet or a multithreaded application, you must use explicit connections, which are explained in Section "5.2 Being Well Connected — Explicitly".
      • Oracle.connect() has —by default— auto-commit turned off. You must issue a SQL COMMIT statement to make any changes permanent. The JDBC connection mechanism DriverManager.getConnection() turns —by default— auto-commit on.
      1. (*) Try to connect with the following JDBC URLs and observe what happens. Explain.
        jdbc:notoracle:oci8:@
        jdbc:oracle:oci:@
      2. (*) What happens when you connect a second time using the Oracle.connect() method? Is a new connection established, or do you continue to be connected with the original connection? Write a sqlj program to find out!
      3. (*) Look at the SQLJ demos in [Oracle Home]/sqlj/demo/. Instead of hard-coding connection parameters in the SQLJ program, these are loaded from a connect.properties file. Rewrite your examples to use this feature.
      4. (***) You can use the same Oracle.connect() and Oracle.close() code both, on the client and in the JServer VM. Explain how this is possible.

      E-mail this page
      Printer View Printer View
      Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy