Hi all,
I Just have to retrieve the data from the oracle database n just write the retrieved data to text file.Please Can anybody send me the code?
Its very urgent. [IMG][/IMG]
Thanks in advance.

Dani AI

Generated

asked for a quick way to pull rows from Oracle and dump them to a text file. As and pointed out, the community favors showing effort; the short example below is intended as a compact, learnable starting point rather than a blind copy/paste solution.

Essential steps: obtain a JDBC Connection, prepare a forward-only read-only statement, set an appropriate fetch size for large results, stream the ResultSet (do not load everything into memory), and write rows with a buffered writer using UTF-8. Handle CLOB/BLOB specially and use simple CSV quoting (or a CSV library) if needed. For older drivers you may need Class.forName("oracle.jdbc.OracleDriver"); modern JDBC drivers are auto-registered.

import java.sql.*;
import java.nio.file.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

String url = "jdbc:oracle:thin:@host:1521:ORCL";
String user = "scott";
String pass = "tiger";
String sql = "SELECT * FROM MY_TABLE";

try (Connection conn = DriverManager.getConnection(url, user, pass);
     PreparedStatement ps = conn.prepareStatement(sql,
         ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
     BufferedWriter w = Files.newBufferedWriter(Paths.get("output.txt"),
         StandardCharsets.UTF_8)) {

    ps.setFetchSize(1000); // tune for your data volume

    try (ResultSet rs = ps.executeQuery()) {
        ResultSetMetaData md = rs.getMetaData();
        int cols = md.getColumnCount();

        for (int i = 1; i <= cols; i++) {
            if (i > 1) w.write(',');
            w.write(md.getColumnLabel(i));
        }
        w.newLine();

        while (rs.next()) {
            for (int i = 1; i <= cols; i++) {
                if (i > 1) w.write(',');
                String v = rs.getString(i);
                if (v != null && (v.contains(",") || v.contains("\"") || v.contains("\n")))
                    v = "\"" + v.replace("\"", "\"\"") + "\"";
                if (v != null) w.write(v);
            }
            w.newLine();
        }
    }
}

Common pitfalls: ClassNotFoundException means the driver JAR is not on the classpath; SQLExceptions often mean bad URL/credentials; very large exports may require increasing fetch size or using server-side tools; CLOBs need getClob()/streaming. For background reading see the Java JDBC tutorial and the Java I/O tutorial. As advised, try this snippet, adapt it to your schema, and post concrete errors if specific troubleshooting is required.

Recommended Answers

All 3 Replies

I think you didn't read the rules of this forum, we help here to people which willing to learn and we do not produce code on demand. There are other sites where they will happily do it for you just don't forget ready your "plastic money"

Is there any way we can filter threads like this from being posted? Or, can they be removed? I'm not sure, but I think DaniWeb could go without such dribble as do-my-homework demands.

hello friend i have also many problems in programs but never think about spoon feeding.n people help me as they can so u should also try by u r on.u will be helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.