Results 1 to 6 of 6

Thread: Insert sql query result in a file

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Insert sql query result in a file

    Hello,
    Here is my problem: I execute an SQL query and the result of the query is inserted in a file. I am trying to do it, but getting error in some conditions, first take a look at my code.
    Code:
    / / Opening FileWriter
    FileWriter fwri = new FileWriter("csv file");
    / / Route lines to update
    while (reset.next()) {
    	/ / Route Tables
    	String userid = reset.getString(1);
    String usr = reset.getString(2);
    System.out.System.out.println("** = Userid" + Userid + "usr =" + usr);	
    					
    / / Elements to be included in the file
    	fwri.write(userid);
    fwri.write(" ");
    fwri.write(usr);
    fwri.write('\ n');
    		}
    / / Close file written
    fwri.close();
    After trying for some time, it seems that the code does not like to work when the query returns a NULL (line column empty). So I did some testing on other non-empty columns and that worked file for me.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Insert sql query result in a file

    Hi,
    You can use the static method String.valueOf () that you return the string "null" if the object is null. This will avoid the exception:
    fw.write( String.valueOf(exmet) );
    Try using this method in your code and then compile and run the program and see if there are any errors in your code, if you find errors in your code, then post then, so that we can help you solving them.

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Insert sql query result in a file

    Hi
    It works fine, but actually the file is empty, instead of having a white / empty file. I value 'null' written in the file would be better for me. Have an idea for you that this null is taken into account as a white / empty as such:
    Grade 1, Grade 2, value3, value 4 = to1,, text, exsometext
    Again thank you for your help. And suggest me if you have an alternative for this.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Insert sql query result in a file

    Hello,
    I think it is possible to do something like this, just have a look at this

    Code:
    while (rs.next()) {
    String usid = rs.getString(1);
    String usr = rs.getString(2);
    String pr = rs.getString(3);
     
    if String.valueOf(i) = null / / Where i is the value of the index columns
    (" ") / / Value to put in the frwri.write
     
    / / Elements to be included in the file
    frwri.write(usid);
    frwri.write(" ");
    frwri.write(usr);
    frwri.write(" ");
    frwri.write(the condition if);
    This is not the complete code, if you need an ideas of how to do it, you can refer to this code.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Insert sql query result in a file

    Hello,
    It works fine, but actually the file is empty, instead of having a white / empty file. I value 'null' written in the file would be better for me.
    I though this is what you want to achieve.
    If you want to avoid this you can see for the part of the code below.
    fw.write( exmet! =null ? exmet: "" );
    Or either this
    if (exmet! =null) {
    fw.write( exmet );
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Insert sql query result in a file

    Hello,
    I am new to java, I have a code written with me, see if this helps you
    Code:
    public static Vector<String> loadFile(String flname){
        Vector<String> strings = new Vector<String>();
        try{
            FileReader fl = new FileReader(flname);
            BufferedReader buf = new BufferedReader(fl);
            while (true)
            {
                String ln = buf.readLine();
                if (ln == null)
                    break;
                else {
                    strings.add(ln); 
                }
            }
            buf.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        return strings;
    }

Similar Threads

  1. Replies: 4
    Last Post: 24-04-2012, 07:53 AM
  2. How to sort the query result in SQL?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 08:45 AM
  3. Difference in SQLite and SQLite3 query result
    By KAILEY in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 04:57 PM
  4. Unable to convert a JDBC Result Set into Xml File
    By Suzane in forum Software Development
    Replies: 3
    Last Post: 23-09-2009, 03:47 PM
  5. Insert PDF File
    By Fanibhushan in forum MS Office Support
    Replies: 3
    Last Post: 06-03-2008, 04:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,677,164.55102 seconds with 17 queries