I nee to get data from database table and i want to store it in file - TechRepublic
Question
October 8, 2008 at 08:47 AM
kh.ramana

I nee to get data from database table and i want to store it in file

by kh.ramana . Updated 17 years, 9 months ago

hi all,

I nee dto store data from database table and i want to store it in file.can any one help me..

sample code———-i have executed this code but it is give some unnecessary symbels in middle…and data is not going to next line….can any one check this and send me the modified code………………

——
import java.io.FileOutputStream;
import java.io.StringReader;
import java.sql.*;

class JdbcEx2
{
public static void main(String as[])
{
try{

Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost/b14db”,”root”,”ramana”);
Statement st=con.createStatement();

//String sql=”insert into b14student values(‘”+sid+”‘,'”+sname+”‘,'”+email+”‘,'”+phone+”‘)”;
String sql=”select * from b14student”;
FileOutputStream fos=new FileOutputStream(“sd1.xls”);
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
int sid=rs.getInt(1);
String sname=rs.getString(2);
String email=rs.getString(3);
long phone=rs.getLong(4);
String s=”\b”+String.valueOf(sid)+”\t”+sname+”\t”+email+”\t”+String.valueOf(phone)+”\t”;
try{
//BufferedInputStream bis=new BufferedInputStream(System.in);

StringReader sr=new StringReader(s);

int x=0;
do{
x=sr.read();

fos.write(x);

//System.out.println();
}while(x!=-1);

}catch(Exception e)
{}

}

//boolean execute()
//Resultset
//System.out.println(x);
}catch(Exception e)
{
e.printStackTrace();
}
}
}

This discussion is locked

All Comments