|
| Database to Excel menggunakan Java | |
| | Pengirim | Message |
---|
ViVaLicious NuuBiiTooL
Jumlah posting : 18 Points : 29 Reputation : 1 Join date : 23.08.11
| Subyek: Database to Excel menggunakan Java Tue Aug 23, 2011 5:00 pm | |
| Iseng iseng di tengah kesibukan, karena ada temen yg nanya jg gmana sih jadiin data yg ada di dalam database menjadi excel make java?. Karena saya pernah buat aplikasinya jadi coba saya share gimana buatnya, yg jelas disini kita harus make librari yg bukan bawaannya jdk, disini kita make librarinya apace poi. Untuk lebih jelasnya bisa baca referensi dari blognya kang eko, kang eko jg sebenarnya sudah buat tutorial kayaa gini, tapi dia buatnya diatas maven, jd yg blum terbiasa make maven bakal bingung jalaninnya gimana bukanya juga gimana.hehehe.. oke ini yg dah saya buat, buat klass klass pendukungnya,, pertama buat databasenya dulu, saya buat dengan nama data. setelah itu buat table dan coba di isi. - Code:
-
01 CREATE TABLE IF NOT EXISTS `excel` ( 02 `id` int(11) NOT NULL AUTO_INCREMENT, 03 `biner` int(11) NOT NULL, 04 PRIMARY KEY (`id`) 05 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 06 07 -- 08 -- Dumping data for table `excel` 09 -- 10 11 INSERT INTO `excel` (`id`, `biner`) VALUES 12 (1, 111101001), 13 (2, 11100100); klass pertama : Data.java - Code:
-
01 package databasetoexcel.model; 02 03 /** 04 * 05 * @author ViVaLicious 06 * 07 */ 08 public class Data { 09 10 private int biner; 11 12 public Data() { 13 } 14 15 public int getBiner() { 16 return biner; 17 } 18 19 public void setBiner(int biner) { 20 this.biner = biner; 21 } 22 23 } kelas ke dua : DataImpl.java - Code:
-
01 package databasetoexcel.model; 02 03 import java.sql.Connection; 04 import java.sql.DriverManager; 05 import java.sql.PreparedStatement; 06 import java.sql.ResultSet; 07 import java.sql.SQLException; 08 import java.sql.Statement; 09 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.logging.Level; 12 import java.util.logging.Logger; 13 14 /** 15 * 16 * @author ViVaLicious 17 * 18 */ 19 public class DataImpl { 20 21 private Connection conn; 22 23 public DataImpl() { 24 try { 25 Class.forName("com.mysql.jdbc.Driver").newInstance(); 26 String db = "jdbc:mysql://localhost:3306/data"; 27 String user = "root"; 28 String pass = "root"; 29 conn = DriverManager.getConnection(db, user, pass); 30 } catch (SQLException ex) { 31 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 32 } catch (ClassNotFoundException ex) { 33 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 34 } catch (InstantiationException ex) { 35 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 36 } catch (IllegalAccessException ex) { 37 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 38 } 39 } 40 41 public void insertDb(int biner){ 42 try { 43 PreparedStatement ps = conn.prepareStatement("insert into excel values(null,?)"); 44 ps.setInt(1, biner); 45 ps.executeUpdate(); 46 } catch (SQLException ex) { 47 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 48 } 49 } 50 51 public List getAll(){ 52 try { 53 Statement st = conn.createStatement(); 54 ResultSet rs = st.executeQuery("select *from excel"); 55 List list = new ArrayList(); 56 while (rs.next()) { 57 Data d=new Data(); 58 d.setBiner(rs.getInt("biner")); 59 list.add(d); 60 } 61 return list; 62 } catch (SQLException ex) { 63 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 64 return null; 65 } 66 } 67 68 public void deleteAll(){ 69 try { 70 PreparedStatement ps = conn.prepareStatement("delete from excel"); 71 ps.executeUpdate(); 72 } catch (SQLException ex) { 73 Logger.getLogger(DataImpl.class.getName()).log(Level.SEVERE, null, ex); 74 } 75 76 } 77 78 } kelas ke tiga :JTableToExcelConverter.java - Code:
-
001 /* 002 * Copyright 2011 Eko Kurniawan Khannedy. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * under the License. 016 */ 017 package databasetoexcel.converter; 018 019 import java.io.File; 020 import java.io.FileOutputStream; 021 import java.io.IOException; 022 import java.util.ArrayList; 023 import java.util.List; 024 import java.util.logging.Level; 025 import java.util.logging.Logger; 026 import javax.swing.JTable; 027 import javax.swing.table.TableModel; 028 import org.apache.poi.hssf.usermodel.HSSFCell; 029 import org.apache.poi.hssf.usermodel.HSSFRow; 030 import org.apache.poi.hssf.usermodel.HSSFSheet; 031 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 032 033 /** 034 * 035 * @author Eko Kurniawan Khannedy 036 */ 037 public class JTableToExcelConverter { 038 039 private JTable jtable; 040 041 public JTableToExcelConverter(JTable table) { 042 this.jtable = table; 043 } 044 045 public void convert(File file) { 046 // ambil table model 047 TableModel tableModel = jtable.getModel(); 048 049 // ambil data header table 050 List header = new ArrayList(); 051 for (int i = 0; i < tableModel.getColumnCount(); i++) { 052 header.add(tableModel.getColumnName(i)); 053 } 054 055 // ambil data seluruh tabel 056 List<List> data = new ArrayList<List>(); 057 058 // ambil data tiap baris 059 for (int i = 0; i < tableModel.getRowCount(); i++) { 060 List row = new ArrayList(); 061 for (int j = 0; j < tableModel.getColumnCount(); j++) { 062 row.add(tableModel.getValueAt(i, j)); 063 } 064 data.add(row); 065 } 066 067 HSSFWorkbook workbook = new HSSFWorkbook(); 068 HSSFSheet sheet = workbook.createSheet(); 069 070 HSSFRow rowHeader = sheet.createRow(0); 071 for (int i = 0; i < header.size(); i++) { 072 HSSFCell rowCell = rowHeader.createCell(i); 073 rowCell.setCellValue(header.get(i)); 074 } 075 076 for (int i = 0; i < data.size(); i++) { 077 HSSFRow row = sheet.createRow(i + 1); 078 List dataRow = data.get(i); 079 for (int j = 0; j < dataRow.size(); j++) { 080 HSSFCell cell = row.createCell(j); 081 cell.setCellValue(dataRow.get(j).toString()); 082 } 083 } 084 085 FileOutputStream stream = null; 086 try { 087 stream = new FileOutputStream(file); 088 workbook.write(stream); 089 } catch (IOException ex) { 090 Logger.getLogger(JTableToExcelConverter.class.getName()).log(Level.SEVERE, null, ex); 091 } finally { 092 if (stream != null) { 093 try { 094 stream.close(); 095 } catch (IOException ex) { 096 Logger.getLogger(JTableToExcelConverter.class.getName()).log(Level.SEVERE, null, ex); 097 } 098 } 099 } 100 } 101 } setelah itu buat form nya. saya beri nama ViewDataToExcel.java screenshot dibawah. untuk lbh jelas sourcodenya download aja di [You must be registered and logged in to see this link.]jfilechooser ini sceen shotnya ketika menambahkan jfilechooser, kalo caranya g kaya gitu nanti hasinya mawot di framenya hehehe ini acara menambahkan librari poi.jar nya. librarinya ada di folder /lib yg di download. Silahkan di download dan di plajari. Semoga bermanfaat.. bagi yang kurang jelas silakan balas thread ini. | |
| | | Fonearena NuuBiiTooL
Jumlah posting : 14 Points : 33 Reputation : -2 Join date : 15.08.11
| Subyek: Re: Database to Excel menggunakan Java Wed Aug 24, 2011 7:39 pm | |
| kreatiff | |
| | | | Database to Excel menggunakan Java | |
|
Similar topics | |
|
| Permissions in this forum: | Anda tidak dapat menjawab topik
| |
| |
| Latest topics | » Baktrack TutorialSun Jul 28, 2019 2:26 am by kenta » aplikasi gambas pada linuxTue Apr 30, 2019 10:28 am by kenta » beli linux ubuntu terbaru di surabayaSun Mar 31, 2019 10:08 am by kenta » desain robotFri Jan 19, 2018 1:25 pm by kenta » membuat robot tidak susahFri Jan 19, 2018 1:15 pm by kenta » Salam.. Salam.. Salam..Thu Nov 30, 2017 7:42 am by BumiayuKita» teknologi penyaring udara dan airWed Oct 04, 2017 8:41 am by kenta » [CloudMILD] VPS SSD IIX 2X RAM + Xtra SSD SpaceMon Jul 24, 2017 10:46 am by BumiayuKita» cara menutup akses dari situs negatifTue Apr 04, 2017 1:04 pm by kenta » Aplikasi Google TalkMon Mar 20, 2017 3:00 am by BumiayuKita» Driver buat Webcam PC ?? merknya M-Tech,, Fri Jan 30, 2015 8:51 pm by aelgrim » Portal Blog,,,,,Sun Dec 14, 2014 12:38 am by robofics» Appteknodroid - Seputar Dunia AndroidMon Nov 10, 2014 11:32 pm by Pr0phecy » Software animasi yang agan2 pakeTue Sep 30, 2014 1:11 pm by X_campus » INDO BILLING 6.70 + KEYSun Sep 21, 2014 2:17 pm by abdul halim |
Statistics | Total 12294 user terdaftar User terdaftar terakhir adalah Adlygans
Total 31710 kiriman artikel dari user in 5734 subjects
|
Banner Forum | Dukung forum Blackc0de dengan memasang bannernya.
|
Social Networking |
|
|