【问题标题】:How to connect to Oracle DB using JDBC connection with out opening Oracle in my machine如何使用 JDBC 连接连接到 Oracle DB 而无需在我的机器上打开 Oracle
【发布时间】:2013-02-13 10:01:43
【问题描述】:

您好 下面是我编写的使用 JDBC 连接连接到 Oracle DB 并返回一些值的代码。但是如果我在我的机器上打开 oracle toad,这段代码会建立连接并返回结果。

但是当 oracle toad 关闭并尝试运行这段代码时,它不会连接。

请告诉我如何在不手动打开 oracle toad 的情况下连接到 oracle DB。

package library;
import java.io.IOException;
import java.sql.*;

public class DBAutomationConnection {
public static void main(String args[]) throws ClassNotFoundException, IOException, SQLException {
  DBAutomationConnection dbconn = new DBAutomationConnection();
  //Connection conn = dbconn.DBConnection1();
  dbconn.DBConnection1("select * from employee where empid='test123'","ROLE_NAME");

}

public void DBConnection1(String query, String colName)throws IOException, ClassNotFoundException{
 Connection connection = null;
 Statement stmt = null;
  try {
      // Load the JDBC driver

      String driverName = "oracle.jdbc.driver.OracleDriver";

      Class.forName(driverName);

      connection = DriverManager.getConnection("jdbc:oracle:thin:@//testhostname:1528/ServiceName", "XXAAA_U", "Jw9S");
      System.out.println("Connection successful: " +connection);


      try {
              stmt = connection.createStatement();
              ResultSet rs = stmt.executeQuery(query);
              while (rs.next()) {
                    //String UserID = rs.getString("USER_ID");
                  String UserID = rs.getString(colName);
                    System.out.println(UserID);     
              }
        } catch (SQLException e ) {
              System.out.println("Could not execute query.");
              //JDBCTutorialUtilities.printSQLException(e);
        } finally {
              if (stmt != null) { stmt.close(); }
        }


  } catch (SQLException e) {
                  System.out.println("Could not connect to the database");
  }

}

【问题讨论】:

    标签: java oracle jdbc


    【解决方案1】:

    您应该从您的操作系统设置>服务中初始化 Oracle TNS-Listener 服务。您可能需要检查您的 tns 配置。

    【讨论】:

      【解决方案2】:

      您需要在系统中安装 ORACLE 客户端才能远程连接 oracle。

      http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

      【讨论】:

        【解决方案3】:

        您的系统中是否安装了 Oracle Thin 驱动程序?这个link可以指导你。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-05-16
          • 2020-08-07
          • 1970-01-01
          • 2021-01-23
          • 2014-09-14
          • 2023-04-04
          • 2016-06-23
          相关资源
          最近更新 更多