Solved

Stream from SQL RS to a file

Posted on 2004-04-29
7
314 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 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