Solved

Stream from SQL RS to a file

Posted on 2004-04-29
7
316 Views
Last Modified: 2010-03-31
Hi,

I am currently making a program, which is getting data from SQL Server DB (using jTDS) into a resultset. This resultset is then looped for every row and then written to a random access file. Now my question is, how I could do this so the program could just dump all the data to a file without any looping? I was thinking of using a direct stream from input to output, this is possible, right?

See below the code (it's working, but it's extremely slow in bigger amount of SPs and not all characters are written to the file as they should be)...

 
<--- begin of code --->

import net.sourceforge.jtds.jdbc.Driver;
import java.io.*;
import java.sql.*;
public class CreateFiles {
      public static void main(String[] args) throws Exception {
            try
            {
                  String url="jdbc:jtds:sqlserver://mssql-server:1433";
                  String usr = "sql";
                  String passwd = "sql";
                  Class.forName("net.sourceforge.jtds.jdbc.Driver");
                  Connection conn=DriverManager.getConnection(url, usr, passwd);
                  conn.setCatalog("TestDB");
                  ResultSet rs = null;
                  CallableStatement proc = null;
                  String fileName = "";
                  String theData = "";
                  fileName = "FirstFile";

                  proc = conn.prepareCall("{ call sp_Java_GetTest  }");
                  rs = proc.executeQuery();
                  while(rs.next())
                  {
                        theData = theData + rs.getString(1) + rs.getString(2)
                        + rs.getString(3) + rs.getString(4);
                  }
                  rs.close();
                  rs = null;

                  proc = conn.prepareCall("{ call sp_Java_GetTest2  }");
                  rs = proc.executeQuery();
                  while(rs.next())
                  {
                        theData = theData + rs.getString(1) + rs.getString(2)
                        + rs.getString(3) + rs.getString(4) + rs.getString(5);
                  }
                  rs.close();
                  rs = null;


                  writeFile(fileName, theData);
                  theData = "";



    public static void writeFile(String f, String s) throws IOException
    {
            try
            {
                  RandomAccessFile rf = new RandomAccessFile(f, "rw");
                  rf.writeBytes(s);
                  rf.close();
            }
            catch (IOException e)
            {
                  System.err.println (e);
            }
      }

}

<--- end of code --->

So as you can see, the idea is to write to one file data from different SPs.

Hope you can help me out with this one!
0
Comment
Question by:planeman
  • 4
7 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 10947885
You could loop thru the result set and write to say a FileOutputStream .
You can't avoid looping thru the result set thought, that is how results are read.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 125 total points
ID: 10948489
If the RS is very huge, you could read it initally into a CachedRowSet and then disconnect from the database. Scrolling through the CachedRowSet would be faster, but well - you have to scroll ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11683278
Please proceed with that recommendation.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11687442
Why a C :-( ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11703371
Thanks, modulo.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 69
jdbc error in jsp application 20 64
hibernate example for saving data 19 56
throw exception 21 58
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question