First, I tried to connect Oracle to Java using this code and it works.
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.sql.*;
- import java.io.*;
- public class TestDBOracle {
- public static void main(String[] args) throws ClassNotFoundException, SQLException
- {
- Class.forName("oracle.jdbc.OracleDriver");
- String url = "jdbc:oracle:thin:@MY-PC:1521:XE";
- Connection conn = DriverManager.getConnection(url,"user", "pwd");
- conn.setAutoCommit(false);
- Statement stmt = conn.createStatement();
- ResultSet rset = stmt.executeQuery("select NC_ID from NC_TEST");
- while (rset.next()) {
- System.out.println (rset.getString(1));
- }
- stmt.close();
- System.out.println ("Ok.");
- }
- }
Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnecti on.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(Physi calConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnect ion.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnectio n(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriv er.java:801)
at java.sql.DriverManager.getConnection(DriverManager .java:582)
at java.sql.DriverManager.getConnection(DriverManager .java:185)
at ExtIndex.main(ExtIndex.java:26)
I used this code.
Expand|Select|Wrap|Line Numbers
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- public class ExtIndex {
- public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException
- {
- Class.forName("oracle.jdbc.OracleDriver");
- String url = "jdbc:oracle:thin:@MY-PC:1512:XE";
- Connection conn = DriverManager.getConnection(url,"user","pwd");
- conn.setAutoCommit(false);
- //------------------------------------------------------------------------//
- Statement stmt = conn.createStatement();
- ResultSet rset = stmt.executeQuery("select NC_ID from NC_TEST");
- File file = new File("indexList.txt");
- FileWriter outputFile = new FileWriter("indexList.txt");
- BufferedWriter fos = new BufferedWriter(outputFile);
- String writeOut;
- try{
- while (rset.next()) {
- System.out.println (rset.getString(1));
- writeOut = rset.getString(1) + "\n";
- fos.write(writeOut);
- }
- }
- catch (FileNotFoundException e) {
- System.err.println("FileStreamsTest: " + e);
- }
- catch (IOException e) {
- System.err.println("FileStreamsTest: " + e);
- }finally {
- try {
- if (fos != null) {
- fos.flush();
- fos.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- stmt.close();
- System.out.println ("Index list generation successful");
- ExtFile.ExtFile();
- //--------------------------------------------------------------------------//
- }
- }