Results 1 to 5 of 5
Thread: unreported exception -- reiro
- 08-23-2010, 02:46 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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
-
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!
- 08-23-2010, 03:15 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 08-23-2010, 03:35 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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"));
- 08-23-2010, 03:45 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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
-
unreported exception (socket connection)
By Symbiot in forum New To JavaReplies: 7Last Post: 05-28-2010, 11:13 AM -
unreported IOException problem
By doha786 in forum New To JavaReplies: 5Last Post: 04-20-2010, 11:14 PM -
Unreported exception java.sql.SQLException
By javamula in forum AWT / SwingReplies: 4Last Post: 09-29-2009, 02:32 PM -
unreported exception IOException -- Yet I AM catching it?
By Agathorn in forum New To JavaReplies: 2Last Post: 09-18-2009, 11:22 PM -
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks