By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

How To Extract File From Oracle 10g Database Using Java

P: 2
Hello, I am trying to extract the data from my Oracle 10g database using Java.

First, I tried to connect Oracle to Java using this code and it works.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.sql.*;
  3. import java.io.*;
  4. public class TestDBOracle {
  5.  
  6.   public static void main(String[] args) throws ClassNotFoundException, SQLException
  7.   {
  8.     Class.forName("oracle.jdbc.OracleDriver");
  9.  
  10.     String url = "jdbc:oracle:thin:@MY-PC:1521:XE";
  11.  
  12.     Connection conn = DriverManager.getConnection(url,"user", "pwd");
  13.  
  14.     conn.setAutoCommit(false);
  15.     Statement stmt = conn.createStatement();
  16.  
  17.     ResultSet rset = stmt.executeQuery("select NC_ID from NC_TEST");
  18.  
  19.     while (rset.next()) {
  20.          System.out.println (rset.getString(1));  
  21.     }
  22.     stmt.close();
  23.     System.out.println ("Ok.");  
  24.   }
  25. }
  26.  
Next, what I intend to do is to generate a text file from ORACLE. However I get this error.

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
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class ExtIndex {
  6.  
  7. public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException
  8. {
  9.  
  10. Class.forName("oracle.jdbc.OracleDriver");
  11. String url = "jdbc:oracle:thin:@MY-PC:1512:XE";
  12.  
  13. Connection conn = DriverManager.getConnection(url,"user","pwd");
  14. conn.setAutoCommit(false);
  15.  
  16.  
  17.  
  18. //------------------------------------------------------------------------//
  19. Statement stmt = conn.createStatement();
  20. ResultSet rset = stmt.executeQuery("select NC_ID from NC_TEST");
  21.  
  22. File file = new File("indexList.txt");
  23. FileWriter outputFile = new FileWriter("indexList.txt");
  24. BufferedWriter fos = new BufferedWriter(outputFile);
  25. String writeOut;
  26.  
  27. try{
  28. while (rset.next()) {
  29. System.out.println (rset.getString(1));
  30. writeOut = rset.getString(1) + "\n";
  31. fos.write(writeOut);
  32. }
  33. }
  34. catch (FileNotFoundException e) {
  35.         System.err.println("FileStreamsTest: " + e);
  36.     } 
  37. catch (IOException e) {
  38.         System.err.println("FileStreamsTest: " + e);
  39.     }finally {
  40.  
  41.             try {
  42.                 if (fos != null) {
  43.                     fos.flush();
  44.                     fos.close();
  45.                 }
  46.             } catch (IOException e) {
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.  
  51.  
  52.  
  53. stmt.close();
  54. System.out.println ("Index list generation successful");
  55.  
  56. ExtFile.ExtFile();
  57. //--------------------------------------------------------------------------//
  58. }
  59.  
  60. }
  61.  
Thanks for any response.
Mar 29 '11 #1
Share this Question
Share on Google+
1 Reply


P: 5
Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection.

I think there is a connection leak in your code.

Also I did not get any code which is closing the connection.
Apr 14 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.