Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:51:29+00:00 2026-06-14T11:51:29+00:00

I am connecting to an Oracle database and querying multiple tables. My current code

  • 0

I am connecting to an Oracle database and querying multiple tables. My current code creates the connection and calls a PL/SQL function which contains the query. Once I have the result set, I add it to a Vector (as I am unsure the number of records each query will result in).

My problem is that I am unsure how to write a delimited file from the Vector. I imagine once I have added my result set to it, it is simply one gigantic string. I need to be able to receive each field from the query and delimit between them, as well as keep rows separate.

Here is my code:

 public static void main(String[] args) throws SQLException {

    // instantiate db connection
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }catch (Exception e) {
        throw new SQLException("Oracle JDBC is not available", e);
    }

    // define connection string and parameters
    String url = "jdbc:oracle:thin:@//host:port/sid";
    Connection conn = DriverManager.getConnection(url, "USERNAME","PASSWORD");


    CallableStatement stmt = conn.prepareCall("{? = CALL <functionname>(?)}");

    // get result set and add to a Vector
    ResultSet rs = stmt.executeQuery();
    Vector<String> results = new Vector();
    while ( rs.next() ){
        results.add(rs.getString(1));
    }

    // close result set, sql statement, and connection
    rs.close();
    stmt.close();
    conn.close();

    // write Vector to output file,
    // where the file name format is MMddyyyy.txt
    try {

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    String dateStr = sdf.format(cal.getTime());

    FileWriter fwrite = new FileWriter(dateStr + ".txt");
    BufferedWriter out = new BufferedWriter(fwrite);

    for(int i = 0; i < results.size(); i++)
    {
        String temp = results.elementAt(i);
        out.write(temp);
    }

    out.close();

}catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}
}

I am just unsure how to go about getting the information from the db and writing it to a delimited file. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:51:31+00:00Added an answer on June 14, 2026 at 11:51 am

    If you are unsure about the number of fields in each of your rows, then probably, it won’t be possible. Because to fetch all the field values from database, you need to know what is the type of each fields, and the number of fields.

    But, I’ll post an example for when you have fixed number of fields, that you know.

    Suppose you have 4 columns per row. Now to display it in tabular form, you would have to use List of List.

    If you are using Vector, use Vector of List.

    Here’s an example for List of List: –

    List<List<String>> results = new ArrayList<List<String>>();
    
    while ( rs.next() ) {
        List<String> tempList = new ArrayList<String>();
        tempList.add(rs.getString(1));
        tempList.add(rs.getString(2));
        tempList.add(rs.getString(3));
        tempList.add(rs.getString(4));
    
        results.add(tempList);
    }
    

    Then to print it, use this loop: –

    for (List<String> innerList: results) {
        for (String fields: innerList) {
            System.out.print(fields + "\t");
        }
        System.out.println();
    }
    

    You can write it in the same form to your BufferedWriter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm connecting to an Oracle database with SQL Developer, the connection data was provided
I am connecting to an oracle database using a connection pool. I have set
Seeing some strange behavior, whereby connecting to Oracle database, and then calling external function,
I have some deprecated code in visual basic when connecting to an oracle database.
The PHP function oci_connect (which connects to an Oracle database) just returns false if
Actually by type 4 db connection from java i am connecting to oracle database
I have an Oracle database that I am connecting to with PHP OCI8. One
I have an Oracle Connection to a ORACLE 10.2 database and want to listen
Is it possible to specify connection/query timeout for the Oracle database queries? Either on
I am having issue connecting from ROR 3.2 to Oracle database. irb(main):001:0> gem 'ruby-oci8',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.