「备份远程数据库java」远程备份oracle数据库

博主:adminadmin 2022-12-09 12:57:05 61

今天给各位分享备份远程数据库java的知识,其中也会对远程备份oracle数据库进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

怎么用java对sql server进行远程备份

//检查磁盘

File file =new File("C:\\backup");    

//如果文件夹不存在则创建    

if  (!file .exists()   !file .isDirectory())      

{       

    System.out.println("//不存在");  

    file .mkdir(); 

    //调用一个执行存储过程方法   

} else   

{  

    System.out.println("//目录存在");  

    //调用一个执行存储过程方法

}  

//写一个方法,执行一个存储过程,该存储过程为备份数据库用的。

java备份远程数据库

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.filechooser.FileFilter;

import java.sql.*;

class DataBackup implements ActionListener

{ JFrame f = null;

JLabel label = null;

JTextArea textarea = null;

JFileChooser fileChooser = null;

public DataBackup()

{

f = new JFrame("FileChooser Example");

Container contentPane = f.getContentPane();

textarea = new JTextArea();

JScrollPane scrollPane = new JScrollPane(textarea);

scrollPane.setPreferredSize(new Dimension(350,300));

JPanel panel = new JPanel();

JButton b1 = new JButton("恢复数据");

b1.addActionListener(this);

JButton b2 = new JButton("备份数据");

b2.addActionListener(this);

panel.add(b1);

panel.add(b2);

label = new JLabel(" ",JLabel.CENTER);

contentPane.add(label,BorderLayout.NORTH);

contentPane.add(scrollPane,BorderLayout.CENTER);

contentPane.add(panel,BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)

{

//windows效果

try

{

//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch(Exception e1)

{

System.out.println("Look and Feel Exception");

System.exit(0);

}

File file = null;

int result;

fileChooser = new JFileChooser("d:\\");

fileChooser.addChoosableFileFilter(new JAVAFileFilter("bak"));

//恢复数据库操作

if (e.getActionCommand().equals("恢复数据"))

{

fileChooser.setApproveButtonText("确定");

fileChooser.setDialogTitle("打开文件");

result = fileChooser.showOpenDialog(f);

textarea.setText("");

if (result == JFileChooser.APPROVE_OPTION)

{

file = fileChooser.getSelectedFile();

}

else if(result == JFileChooser.CANCEL_OPTION)

{

}

/***************执行事件*******************/

//在这里写恢复数据库事件

try

{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

}

catch(ClassNotFoundException error)//驱动加载失败

{

System.err.println("驱动加载失败");

}

//连接到数据库

Connection conStudent;

try

{

conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=master","sa","");

Statement cmdStudent = conStudent.createStatement();

cmdStudent.execute("Restore Database HomeMIS from Disk='"+file.getPath()+"'");

if(conStudent != null)

{//关闭数据库连接

cmdStudent.close();

conStudent.close();

}

}

catch(SQLException sqlerr)

{

System.out.println("Error:"+sqlerr.toString());

}

}

//备份数据库操作

if (e.getActionCommand().equals("备份数据"))

{

result = fileChooser.showSaveDialog(f);

file = null;

String fileName;

if (result == JFileChooser.APPROVE_OPTION)

{

file = fileChooser.getSelectedFile();

String fileName1 = file.getName();

String filePath = file.getPath();

int index = fileName1.lastIndexOf('.');

if (index 0)

{

String extension = fileName1.substring(index+1).toLowerCase();

if(!extension.equals("bak"))

{

filePath = filePath + ".bak";

}

}

if (index 0)

{

filePath = filePath + ".bak";

}

/***************执行事件*******************/

//在这里写备份数据库事件

//装入JDBC驱动

try

{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

}

catch(ClassNotFoundException error)//驱动加载失败

{

System.err.println("驱动加载失败");

}

//连接到数据库

Connection conStudent;

try

{

conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=pubs","sa","");

Statement cmdStudent = conStudent.createStatement();

cmdStudent.execute("Backup Database HomeMIS To Disk='"+filePath+"'");

if(conStudent != null)

{//关闭数据库连接

cmdStudent.close();

conStudent.close();

}

}

catch(SQLException sqlerr)

{

System.out.println("Error:"+sqlerr.toString());

}

}

else if(result == JFileChooser.CANCEL_OPTION)

{

}

}

}

} //过滤文件

class JAVAFileFilter extends FileFilter

{ String ext;

public JAVAFileFilter(String ext)

{

this.ext = ext;

}

public boolean accept(File file)

{

if (file.isDirectory())

return true;

String fileName = file.getName();

int index = fileName.lastIndexOf('.');

if (index 0 index fileName.length()-1) {

String extension = fileName.substring(index+1).toLowerCase();

if (extension.equals(ext))

return true;

}

return false;

}

public String getDescription(){

if (ext.equals("bak"))

return "Data Bakeup File (*.bak)";

return "";

}

public static void main(String[] args)

{

new DataBackup();

}

}

我现在需要用java做一项目,用于备份各种数据库。

1、使用SQLPLUS或者PLSQL

2、dos命令行中敲如下命令

exp 用户名/密码@sid_ip file=d:\back.dmp

sid 为服务器端的服务,ip为服务器的IP地址

前提你要装个Oracle 客户端,并且配好与服务器的连接

用户必须拥有dba的权限

详细:

exp 用户名/密码@要连接的远程计算机IP/要备份的远程数据库名称 file=文件路径

举例:

exp hom/hom@192.168.5.14/qa file=d:\aa1.dmp

不会看不懂吧

备份远程数据库java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于远程备份oracle数据库、备份远程数据库java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-09,除非注明,否则均为首码项目网原创文章,转载请注明出处。