eclipse4.42でjre1.8を使用し、oracle12cに接続しようとしています。
下記のコードは参考リンクで取得し、実際にeclipseで試してみましたがこのようなエラーが表示されます。
データベース上に 表明EMPと列名ENPNO,ENAME,JOBの3点は作成済みです。
リンク先のコードは正しいものとして自分が間違っている点としてはSIDの記入間違い、サーバー名が間違っている。 2点が考えられます。
以上の2点についてコマンドプロンプトからsqlplusにログインしselectコマンドを使ってSID,サーバー名を確認してみましたが間違っているかもしれません。
なのでSID検索方法とサーバー名に当たるものを確認する方法をお教えてください。
以上2点以外で怪しい点、他に改善すべき点があれば教えてください。よろしくお願いします。
java.sql.SQLException: Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DataAccess { public static void main(String[] args) { DataAccess dataAccess = new DataAccess(); try { dataAccess.selectOracle(); } catch (Exception e) { e.printStackTrace(); } } public void selectOracle() throws Exception { // ユーザ名 String user = "scott"; // パスワード String pass = "tiger"; // サーバ名 String servername = "localhost"; // SID String sid = "orcl"; Connection conn = null; Statement stmt = null; ResultSet rset = null; try { // JBBCドライバクラスのロード Class.forName("oracle.jdbc.driver.OracleDriver"); // Connectionの作成 conn = DriverManager.getConnection("jdbc:oracle:thin:@" + servername + ":1521:" + sid, user, pass); // Statementの作成 stmt = conn.createStatement(); // Resultsetの作成 rset = stmt.executeQuery("select EMPNO, ENAME, JOB from EMP"); // 取得したデータを出力する while (rset.next()) { System.out.println(rset.getString("EMPNO") + "," + rset.getString("ENAME") + "," + rset.getString("JOB")); } } catch (ClassNotFoundException e) { throw e; } catch (SQLException e) { throw e; } catch (Throwable e) { throw e; } finally { try { /* クローズ処理 */ if (rset != null) { rset.close(); rset = null; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (Throwable e) { // nop } } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/11/01 01:36
2017/11/01 06:31
退会済みユーザー
2017/11/01 06:52
2017/11/01 07:37
退会済みユーザー
2017/11/01 08:03