September 09, 2010, 01:21:38 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Read from database and write to excel  (Read 923 times)
0 Members and 1 Guest are viewing this topic.
G. Ritardo
Member
*
*

Reputation: 0
Offline Offline
Posts: 53
Referrals: 0

Awards
« on: May 31, 2009, 07:25:32 PM »

import java.sql.*;
import java.io.*;

public class Rgupta31 {

    public static void main(String[] args) {

        try {
           
            String newLine = System.getProperty("line.separator");
           
            Writer output = null;
            File file = new File("ExcelFile.csv");
            output = new BufferedWriter(new FileWriter(file));
                                   
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:schooldb");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("Select * from [Sheet1$]");

            ResultSetMetaData rsmd = rs.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();

            while (rs.next()) {

                for (int i = 1; i <= numberOfColumns; i++) {
                    if (i > 1)
                        System.out.print(", ");
                    String columnValue = rs.getString(i);
                   
                    System.out.print(columnValue);
                    output.write(columnValue);
                    output.write(newLine);
                   
                }
                System.out.println("");
            }

            st.close();
            con.close();
            output.close();

        } catch (Exception ex) {
            System.err.print("Exception: ");
            System.err.println(ex.getMessage());
        }
    }
}
Logged
My Contributions

Onzin > Email address of gaylords


Founder of www.Retardation-Foundation.com :: A community for retarded people!? Powered by admin Gaylord Ritardo.
Javaforums.net :: a community about Java software development.
« on: May 31, 2009, 07:25:32 PM »

 Logged
ashish
Member
*

Reputation: 0
Offline Offline
Posts: 27
Referrals: 0

Awards
« Reply #1 on: August 12, 2010, 01:44:54 AM »

yes it is good program. dance dance dance
import java.sql.*;
import java.io.*;

public class Rgupta31 {

    public static void main(String[] args) {

        try {
            
            String newLine = System.getProperty("line.separator");
            
            Writer output = null;
            File file = new File("ExcelFile.csv");
            output = new BufferedWriter(new FileWriter(file));
                                    
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:schooldb");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("Select * from [Sheet1$]");

            ResultSetMetaData rsmd = rs.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();

            while (rs.next()) {

                for (int i = 1; i <= numberOfColumns; i++) {
                    if (i > 1)
                        System.out.print(", ");
                    String columnValue = rs.getString(i);
                    
                    System.out.print(columnValue);
                    output.write(columnValue);
                    output.write(newLine);
                    
                }
                System.out.println("");
            }

            st.close();
            con.close();
            output.close();

        } catch (Exception ex) {
            System.err.print("Exception: ");
            System.err.println(ex.getMessage());
        }
    }
}
regards,
phe9oxis,

« Last Edit: August 12, 2010, 02:22:14 AM by Arkie » Logged
Javaforums.net :: a community about Java software development.
   

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.254 seconds with 36 queries.

Google visited last this page August 29, 2010, 07:01:56 PM