473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How To Extract File From Oracle 10g Database Using Java

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
1 3900
sanat
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

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

Similar topics

0
by: otto | last post by:
Hi, all: We have a Windows application that make operations against an Oracle database. The business logic components are in the server and are accesible from the client interface trought a...
0
by: Asvineo | last post by:
Hi Can anyone please tell me how to connect an Oracle database to a dll written in VC++.Please also mention how to create Connection object,recordset for the same.The dll is an MFC based dll.Please...
2
by: Steve Kershaw | last post by:
I have a need to extract data from an Oracle database using an SQL query. The problem is that I need to insert a parameter into the query. For example: SELECT EMPLOYEE, DED_CODE FROM QUARTDED...
2
by: Hatricks | last post by:
Hi, How can I store & retrieve PDF file in SQL 2005 database using java. Any code will be of great help.
1
by: Master Ken | last post by:
Hi All, I'm very new to C# and ASP and I am asking for some help as I don't really know where to start on this project. Some background info first I run a report each week which outputs data...
2
by: mannavavp | last post by:
hi friends, please let me know how i can store audio files in database table using java programming?
3
by: techquest | last post by:
Hi, I want to connect into oracle database and export the table data into a flat file using UNIX shell scripts. I cant use other GUI tools to do this, as the dataload will be in millions. hence if...
5
by: sumanta123 | last post by:
Dear Sir, How to export/import the excel file in oracle database using sql promt. Please guide me the command(export/import) for the neddful. Thanks in Adavance. Regards Sumanta Panda
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.