17 Eylül 2012, 09:35
|
#1 |
Çevrimdışı
Kullanıcıların profil bilgileri misafirlere kapatılmıştır.
| Java ile MySQL Backup Alma işlemi
Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir. PHP Kod: Kodu kopyalamak için üzerine çift tıklayın!
import org.apache.commons.io.FileUtils; public class dbasebackupdetailpanel extends javax.swing.JPanel { JFileChooser fc = new JFileChooser(); private boolean isConnected = false; Connection conn = null; List tables = null; String url, password, uname; //backup butonu private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) { try { fc.setDialogTitle("Create a file to BackUp database"); fc.setCurrentDirectory(fc.getCurrentDirectory()); if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File file = new File(fc.getSelectedFile().getAbsolutePath() + ".sql"); String data = null; if (chkTables.isSelected() == false) { data = this.getData(tfHost.getText(), tfPort.getValue().toString(), tfUser.getText(), tfPassword.getText(), "dbFStation"); } else { data = this.getData(tfHost.getText(), tfPort.getValue().toString(), tfUser.getText(), tfPassword.getText(), "dbFStation",cmbTables.getSelectedItem().toString()); } FileUtils.writeStringToFile(file, data);//avaible when import org.apache.commons.io.FileUtils; xputils.showMessage("Backup Successfull"); } } catch (Exception e) { xputils.showErrorMessage(e.toString()); }} //baglantı butonu private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) { //kontrol validasyonu url = "jdbc:mysql://" + tfHost.getText() + ":" + tfPort.getValue() + "/dbFstation"; uname = tfUser.getText(); password = tfPassword.getText(); try { conn = DriverManager.getConnection(url, uname, password); if (conn != null) { isConnected = true; this.btnBackup.setEnabled(true); getTableNames(conn); this.cmbTables.setModel(tablesModel()); xputils.showMessage("Connection was Succesfull"); } else { this.btnBackup.setEnabled(false); isConnected = false; xputils.showMessage("Connection was not Succesfull"); } } catch (SQLException ex) { Logger.getLogger(dbasebackupdetailpanel.class.getName()).log(Level.SEVERE, null, ex); }} private int BUFFER = 10485760; //butun tabloların backup ını almak private String getData(String host, String port, String user, String password, String db) throws Exception { Process run = Runtime.getRuntime().exec( "mysqldump –host=" + host + " –port=" + port +" –user=" + user + " –password=" + password + " " + db); InputStream in = run.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuffer content = new StringBuffer(); int count; char[] cbuf = new char[BUFFER]; while ((count = br.read(cbuf, 0, BUFFER)) != -1) { content.append(cbuf, 0, count); } br.close(); in.close(); return content.toString(); } //bir tablonun backup ını almak private String getData(String host, String port, String user, String password, String db, String table) throws Exception { Process run = Runtime.getRuntime().exec( "mysqldump –host=" + host + " –port=" + port + " –user=" + user + " –password=" + password + " " + db + " " + table); InputStream in = run.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuffer content = new StringBuffer(); int count; char[] cbuf = new char[BUFFER]; while ((count = br.read(cbuf, 0, BUFFER)) != -1) { content.append(cbuf, 0, count); } br.close(); in.close(); return content.toString(); } //db'den tablo adlarını almak private void getTableNames(Connection con) { String[] DB_TABLE_TYPES = {"TABLE"}; String COLUMN_NAME_TABLE_NAME = "TABLE_NAME"; ResultSet rs = null; try { DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTables(null, null, null, DB_TABLE_TYPES); if (rs != null) { tables = new ArrayList(); while (rs.next()) { String tableName = rs.getString(COLUMN_NAME_TABLE_NAME); if (tableName != null) { tables.add(tableName); }}} con.close(); } catch (Exception e) { e.printStackTrace(); }} //liste olarak tablo adlarını combobox içine doldurmak public ComboBoxModel tablesModel() { ComboBoxModel model; if (tables == null || tables.size() <= 0) { Object[] d = new Object[1]; ComboBoxModel mo = new DefaultComboBoxModel(d); return mo; } Object[] days = new Object[tables.size()]; int i = 0; try { ListIterator lg = tables.listIterator(); while (lg.hasNext()) { days[i] = tables.get(i); i = i + 1; } } catch (Exception ex) { System.out.println("error occured " + ex.toString()); } model = new DefaultComboBoxModel(days); return model; }
[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...] |
| |