Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
BufferedWriter error
Amar Naik
Ranch Hand
Posts: 43
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
the output of SQL query is around 19254 rows. But my below code writes only 18700 lines to o/p file. Sometimes the o/p file as 18900 rows. No idea why the file write is having error. It does not even throw any exception
public class Application_DBmap { String noofrecords="",dbcount=""; public Application_DBmap(){} /* This is for opening a database connection */ public Connection opendb() { Connection conn; // Load the Oracle JDBC driver try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); conn = DriverManager.getConnection( "jdbc :-o racle:thin:@ip :-P ort:MR","id","pwdl"); } catch (SQLException ex) { return null; } return conn; } /* This is for closing a database connection */ public boolean closedb(Connection connobj) { try { connobj.close(); } catch (SQLException ex) { return true; } return true; } /* this is for finding a data in the index table for Report Class B */ public String CheckCountofRecords(Connection conn) { try { int data_flag=0; String Noofrows=""; String sqlquery = "select count(*) from ALL_TABLES a, mlr.arsseg b , mlr.arsapp c where c.agid=b.agid and a.TABLE_NAME=b.TABLE_NAME"; Statement stmt = conn.createStatement(); System.out.println("2" ); ResultSet rset = stmt.executeQuery(sqlquery); System.out.println("3" ); while (rset.next()) { Noofrows=rset.getString(1); } rset.close(); stmt.close(); return Noofrows; } catch (SQLException ex) { System.out.println("CheckCountofRecords" +ex); return "false"; } } public void Application_DBmap_update(Connection conn,BufferedWriter out) { try { int i=0; String str,tablename="",Applicationame="",table_load_startdt="",table_load_stopdt="",table_load_closedt=""; String sqlquery = "select DISTINCT a.TABLE_NAME,c.NAME,b.start_date,b.stop_date,b.closed_date from ALL_TABLES a, mlr.arsseg b , mlr.arsapp c where c.agid=b.agid and a.TABLE_NAME=b.TABLE_NAME"; Statement stmt = conn.createStatement(); System.out.println("2" ); ResultSet rset = stmt.executeQuery(sqlquery); System.out.println("3" ); while (rset.next()) { i=i+1; System.out.println(i+":"); tablename=rset.getString("TABLE_NAME"); Applicationame=rset.getString("NAME"); table_load_startdt=rset.getString("start_date"); table_load_stopdt=rset.getString("stop_date"); table_load_closedt=rset.getString("closed_date"); out.write(i+":"+tablename+","+Applicationame+","+table_load_startdt+","+table_load_stopdt+","+table_load_closedt+"\n"); } rset.close(); } catch (IOException e) { System.out.println("i/o exception"); } catch (SQLException ex) { System.out.println("CheckCountofRecords" +ex); } } public static void main(String arg[]) { Application_DBmap obj= new Application_DBmap(); Connection conn; String noofrecords="",dbcount=""; try { BufferedReader in = new BufferedReader(new FileReader("dbapplicationmap.txt")); noofrecords=in.readLine();//read the number of tables from first line of i/p file if ((conn = obj.opendb()) == null) { System.out.println("open db error\n"); return; } dbcount=obj.CheckCountofRecords(conn); if (noofrecords.equals(dbcount)) { System.out.println("Count Equal\n"); } else { System.out.println("Count Not Equal\n"); in.close(); BufferedWriter out = new BufferedWriter(new FileWriter("dbapplicationmap.txt")); out.write(dbcount+"\n"); obj.Application_DBmap_update(conn,out); } System.out.println("file count ->"+noofrecords+" "+"db count"+dbcount+"\n"); obj.closedb(conn); in.close(); } catch (IOException e) { System.out.println("i/o exception"); } }//class main end }// class Applcation_DBmap end
Keith Lynn
Ranch Hand
Posts: 2412
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Anytime you write output to a file through a stream, make sure you close the stream when you are finished writing.
Amar Naik
Ranch Hand
Posts: 43
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks. It solved the problem
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Determining null resultset
Stored procedures in JDBC?
Memory issue with Result set
Need Code Review
How to connect to mysql database?
More...