现在的位置: 首页 > 综合 > 正文

Connect to an Oracle database with JDBC

2017年01月11日 ⁄ 综合 ⁄ 共 1398字 ⁄ 字号 评论关闭
 

 

Oracle Corporation has released a free 100% JAVA driver. It is available at their Web site. According to the security rule, the Oracle server must be on the same machine as the Web server, since the communication with the Applet is done through Sockets. This is not always possible, so you will need something called a Connection Manager (from Oracle) or DBAnywhere from Symantec (demo version available for testing) to act as a bridge between the client, the Web server and the Database server.

import java.sql.*;

public class connectToOracle extends java.applet.Applet {
  Driver driver;
  Connection conn = null;
  static String driverUsed =
     "oracle.jdbc.driver.OracleDriver";
  static String serverAddress =
     "jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance";
     // jdbc:oracle:thin is the driver used
     // scott/tiger is user/password
     // www.myServer.com  is  the same machine from where the Applet was loaded
     // 1234  is the port used
     // myInstance  is  where my data is

  public void init(){
    makeConnection(serverAddress);
    }
      
  public void makeConnection(String svr) {
    try {
      System.out.println("Loading ... " + driverUsed);
      driver =
         (Driver)Class.forName(driverUsed).newInstance();
      System.out.println("Connecting ... " + svr);
      conn =
         DriverManager.getConnection(svr);
      System.out.println("Ready.");
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }
   }

NOTE: With Microsoft IEv4, you may have an Exception talking about "No more data on socket", simply *TURN OFF* the JIT to solve the problem.

Also check the parameter sqlnet.expiretime in the SQLNET.ORA file, if there is one try to remove it or increase the value (Oracle 7.3).

 

抱歉!评论已关闭.