?
Solved

Stream from SQL RS to a file

Posted on 2004-04-29
7
Medium Priority
?
324 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 375 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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 9 hours left to enroll

764 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