Solved

Stream from SQL RS to a file

Posted on 2004-04-29
7
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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