How to retrive all the rows in a mysql database and write it to a file using java?
Hai to all,,
i had to tried to retrive and write a single row to a file from the database..
But dont know to write all the retrived rows to a file from the database..
how to do that...
here are my codings for writing a single row to a file...
Code:
import java.io.*;
import java.sql.*;
public class DataBase {
public static void main(String args[]) {
String Driver;
Statement stmt;
ResultSet rs;
Driver = "com.mysql.jdbc.Driver";
Connection con = null;
try {
File file = new File("C:/test.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
Class.forName(Driver);
con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
if(!con.isCloses)
{
System.out.println("Successfully connected to MySQL DataBase \n");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM employees");
InputStream data= null;
System.out.println("Display all results: ");
while (rs.next()) {
int id = rs.getInt("Employee_Number");
String name = rs.getString("First_Name");
String addr = rs.getString("Second_Name");
data = rs.getBinaryStream(1);
int temp=0;
while( (temp = data.read()) != -1) {
out.write(temp); }
System.out.println( id + name + addr);
}
out.flush();
out.close();
}
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
Can any one plzz help me soon...
thanks and regards..
senthil