Monday, August 27, 2007

JDBC Program to write to a Excel File

public class JdbcConnect {
public static void main(String args[]){
try{
Statement st = null;
ResultSet rs = null;
String sql = "select * from tab";
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
System.out.println(" Driver Loaded"); //"jdbc:oracle:thin:@//localhost:1521/XE
DriverManager.getConnection("jdbc:db2://localhost:6789/database","ganesh","ganesh");

System.out.println("Connection Created ");

st = connection.createStatement();
rs = st.executeQuery("select username from ganesh.sample");
System.out.println("LEN:: "+rs.getFetchSize());
StringBuffer sBuffer = new StringBuffer();
while(rs.next()){
sBuffer.append(rs.getString(1));
sBuffer.append("\n");

}

BufferedWriter bw = new BufferedWriter(new FileWriter("test1.xls"));
bw.write("new file excel \n \n "+sBuffer.toString());
bw.close();


}catch(Exception exp){
exp.printStackTrace();
}
}
}

No comments:

Post a Comment