Write data from Database to File in Java -
i trying write data database text file.
the text file remains empty. tried deg. here code:
public void writetext() { try { class.forname("com.mysql.jdbc.driver"); } catch(exception e) { e.printstacktrace(); } final string db_url ="jdbc:mysql://mis-sql.uhcl.edu/gattupalliv3940"; connection conn = null; statement stat = null; resultset rs = null; try { conn = drivermanager.getconnection(db_url,"gattupalliv3940","1552688"); stat = conn.createstatement(); rs = stat.executequery("select * student_2"); filewriter fw = new filewriter("data.txt"); bufferedwriter bw = new bufferedwriter(fw); string line =""; while(rs.next()) { line = rs.getint(1)+"\t"+rs.getstring(2)+"\t"+rs.getdouble(3); bw.write(line); bw.newline(); } bw.close(); } catch(exception e) { e.printstacktrace(); } { try { conn.close(); rs.close(); stat.close(); } catch(exception e) { e.printstacktrace(); } } }
if aren't getting errors , file empty, thing can happening while(rs.next()) line must returning false first time through; meaning aren't getting database.
have stepped through code verify lines executing , ones not?
i suspect file isn't being written expect since aren't specifying directory. created simple test , file written out tomcat's directory @ ...\springsource\vfabric-tc-server-developer-2.6.4.release\spring-insight-instance\wtpwebapps\junk\data.txt (my project named junk). below code (without database stuff):
@requestmapping(value="/") public modelandview test(httpservletrequest request, httpservletresponse response) throws ioexception{ writetext(request.getrealpath("data.txt")); return new modelandview("home"); } public void writetext(string path) { bufferedwriter bw = null; try { filewriter fw = new filewriter(path); bw = new bufferedwriter(fw); string line = "this test"; bw.write(line); bw.newline(); } catch(exception e) { e.printstacktrace(); } { if( bw != null ) { try { bw.close(); } catch (ioexception e) { // ignore } } } }
Comments
Post a Comment