DATABASE TO FILE WITH NEWLINE IN JAVA..
public class FileHandling {
public static void
main(String[] args) throws ClassNotFoundException, SQLException, IOException {
ArrayList<String> dataid = new ArrayList<String>(
Arrays.asList("1","2","3","4","5"));
FileStore(dataid,"File1","SampleFileDb","datas","DYNAMIC
FILE HANDLER","id");
}
public static
void FileStore(List<String> dataid,String filename,String
databasename,String tablename,String title,String colidname) throws
ClassNotFoundException, SQLException, FileNotFoundException, IOException
{
java.util.Date
d=new java.util.Date();
String
time=d.toString();
DateFormat
dateFormat = new SimpleDateFormat("HH-mm-ss");
File fout = new
File(filename+".doc");
FileOutputStream
fos = new FileOutputStream(fout);
BufferedWriter
bw = new BufferedWriter(new OutputStreamWriter(fos));
String
line="****************************************************************";
bw.newLine();
bw.write(title);
bw.newLine();
bw.write(line);
bw.newLine();
bw.write(time);
bw.newLine();
bw.write(line);
bw.newLine();
for(int
i=0;i<dataid.size();i++)
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/"+databasename+"","root","root");
Statement
stm=con.createStatement();
ResultSet
rs=stm.executeQuery("select * from "+tablename+" where
"+colidname+"='"+dataid.get(i)+"'");
bw.newLine();
while(rs.next())
{
String
data1=rs.getString(1);
String
data2=rs.getString(2);
String
data3=rs.getString(3);
String
data4=rs.getString(4);
bw.write(data1+"__"+data2+"__"+data3+"__"+data4);
bw.newLine();
}
}
bw.write(line);
bw.newLine();
bw.newLine();
bw.write(line);
bw.close();
}
No comments:
Post a Comment