Codxplore.com

Latest news: New android application Islamic Video has been released. its a free islamic video application for android smartphone. Download now!


Database class example

public class DB {

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    ResultSetMetaData md = null;
    String dburl = "jdbc:mysql://localhost:3306/addressBook";
    String dbuser = "root";
    String dbpassword = "";

    public DB() {
        try {
            con = DriverManager.getConnection(dburl, dbuser, dbpassword);
            st = con.createStatement();
        } catch (Exception ex) {
            ex.getMessage();
        }
    }
}

 

//button action

DB db = new DB();

            try {
                FileWriter writer = new FileWriter("C:UsersPublicCompany List.csv");

                String sql = "SELECT companyId,companyName,companyAddress,companyPhone,companyMobile,companyEmail FROM tbl_address_list  ";

                db.rs = db.st.executeQuery(sql);
                db.rs = db.st.getResultSet();
                db.md = db.rs.getMetaData();
                int columnCount = db.md.getColumnCount();

                while (db.rs.next()) {
                    for (int i = 1; i <= columnCount; i++) {
                        writer.append(db.rs.getString(i)+',');
                    }
                    writer.append(' ');
                }

                writer.flush();
                writer.close();
                
                JOptionPane.showMessageDialog(rootPane, "Csv file is generated successfully and save into C:UsersPublic");

            } catch (Exception io) {
                System.err.println(io);
            }

Tags :

Views : 640

Subscribe Us


Follow Us