目次へ

解答例 - 実習課題3 - (補足)Oracleへの接続

(実習課題3)

実習課題2のプログラムを改良しなさい。

  • OCI Driverを用いて作成する事。

解答例

/**
 * OracleInsertOci.java TECHSCORE Java JDBC 補足 実習課題3
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *  
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class OracleInsertOci {

    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle.techscore",
                                               "scott",
                                               "tiger");
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("connection success");
        try {
            String sql = "insert into TABLE_A values('123','テスト','2004-12-09')";
            PreparedStatement statement = conn.prepareStatement(sql);
            int result = statement.executeUpdate();
            System.out.println("result:" + result);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {}
        }
    }

}

▼設定ファイル tnsnames.ora

ORACLE.TECHSCORE =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dbserver)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = oracle.techscore)
    )
  )

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp