Results 1 to 5 of 5
  1. #1
    Reiro is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default unreported exception -- reiro

    Hi,

    I get the below error when i compile my code, i am reading a text file with an sql statement, then displaying the statement from file on console and the result (eg:sysdate value) in console, then writing db contents to a csv file:

    TestRW.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
    PrintWriter outputStream = new PrintWriter(new FileWriter("C:\\test\\readsq
    l.csv"));


    Please find my code below:





    import java.sql.*;
    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;


    class TestRW {
    public static void main(String args[]) throws SQLException {

    File file = new File("C:\\test\\readsql.txt");
    PrintWriter outputStream = new PrintWriter(new FileWriter("C:\\test\\readsql.csv"));


    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    String Read = "";

    try {
    fis = new FileInputStream(file);


    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);


    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String serverName = "sugarcrmmtn";
    int port = 0000;
    String user = "user";
    String password = "password";
    String SID = "somedb";
    String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
    Connection conn = DriverManager.getConnection(URL, user, password);
    String SQL = "";
    Statement stat = conn.createStatement();

    while (dis.available() != 0) {

    SQL =dis.readLine();
    System.out.println(SQL);


    ResultSet rs = stat.executeQuery(SQL);

    while (rs.next()) {
    Read = rs.getString (1);
    System.out.println (Read);
    outputStream.println(Read);

    }
    rs.close();


    }
    stat.close();
    conn.close();
    fis.close();
    bis.close();
    dis.close();
    outputStream.close();

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }




    }
    }



    Please help :)... thanking you in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to our forum!

    Please have a look at the Exception section of the Sun / Oracle Java tutorials and you'll see how to catch or throw exceptions since this is what you will need to do (probably catch). it will explain why this is important and will show you how to do this including code examples. Much luck and again welcome!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Reiro View Post
    Hi,

    I get the below error when i compile my code, i am reading a text file with an sql statement, then displaying the statement from file on console and the result (eg:sysdate value) in console, then writing db contents to a csv file:

    TestRW.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
    PrintWriter outputStream = new PrintWriter(new FileWriter("C:\\test\\readsq
    l.csv"));
    Suppose that file can't be opened for writing; the FileWriter constructor will throw an IOException. What does your code do with it? Nothing. You either have to mark your method as such 'throws IOException' or handle the exception.

    kind regards,

    Jos

  4. #4
    Reiro is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi Guys ,


    Thanks, i moved below line into into the TRY and all works fine now:

    PrintWriter outputStream = new PrintWriter(new FileWriter("C:\\test\\readsql.csv"));

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Reiro View Post
    Hi Guys ,


    Thanks, i moved below line into into the TRY and all works fine now:

    PrintWriter outputStream = new PrintWriter(new FileWriter("C:\\test\\readsql.csv"));
    Good; I haven't even seen that your code already anticipated for the IOException because the code was hardly readable (see above). Next time you post code put a [code] tag at the start and a [/code] tag at the end of your code; indendation will be preserved then and no silly smiley faces show up in your code.

    kind regards,

    Jos

Similar Threads

  1. unreported exception (socket connection)
    By Symbiot in forum New To Java
    Replies: 7
    Last Post: 05-28-2010, 12:13 PM
  2. unreported IOException problem
    By doha786 in forum New To Java
    Replies: 5
    Last Post: 04-21-2010, 12:14 AM
  3. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 03:32 PM
  4. Replies: 2
    Last Post: 09-19-2009, 12:22 AM
  5. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •