Solved

Stream from SQL RS to a file

Posted on 2004-04-29
7
308 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:
mayankeagle 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:mayankeagle
ID: 11683278
Please proceed with that recommendation.
0
 
LVL 30

Expert Comment

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

Expert Comment

by:mayankeagle
ID: 11703371
Thanks, modulo.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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.
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now