📄 restore.java
字号:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//数据库恢复到时间点功能
public class restore extends JFrame implements ActionListener{
//定义组件
private JLabel inputPathOfDatabaseLabel=new JLabel("A.输入保存数据库备份的路径及文件名:");
private JTextField inputPathOfDatabase=new JTextField(30);
private JLabel inputPathOfLogsLabel=new JLabel("B.输入保存日志的路径及文件名:");
private JTextField inputPathOfLogs=new JTextField(30);
private JButton restoreButton=new JButton("恢复");
private JButton clearButton=new JButton("清除");
private JLabel noticeLabel1=new JLabel("注意:路径要存在,文件保存类型为.bak");
private JLabel noticeLabel2=new JLabel("注意:路径要存在,文件保存类型为.logs");
private JLabel line1=new JLabel("---------------------------------------------------------------------------------------------------------------------------");
private JLabel line2=new JLabel("---------------------------------------------------------------------------------------------------------------------------");
//定义数据库操作所用变量
Statement st=null;
Connection con=null;
ResultSet rs=null;
private boolean flag=true;
//构造方法
public restore(){
setTitle("数据库恢复");
setSize(500,400);
setLocation(300,400);
setResizable(false);
//创建面板
Container c=getContentPane();
c.setLayout(null);
//提示输入数据库备份路径区
c.add(inputPathOfDatabaseLabel);
inputPathOfDatabaseLabel.setFont(new Font("华文行楷",Font.BOLD,20));
inputPathOfDatabaseLabel.setBounds(40,15,400,40);
c.add(inputPathOfDatabase);
inputPathOfDatabase.setBounds(30,60,400,40);
inputPathOfDatabase.setText("g:\\BSMS\\backup\\BSMS.bak");
c.add(noticeLabel1);
noticeLabel1.setFont(new Font("华文新魏",Font.PLAIN, 18));
noticeLabel1.setBounds(50,100,330,40);
noticeLabel1.setForeground(Color.red);
//分界线
c.add(line1);
line1.setBounds(0,130,500,10);
line1.setForeground(Color.BLUE);
//提示输入备份日志存放路径区
c.add(inputPathOfLogsLabel);
inputPathOfLogsLabel.setFont(new Font("华文行楷",Font.BOLD,20));
inputPathOfLogsLabel.setBounds(40,135,400,40);
c.add(inputPathOfLogs);
inputPathOfLogs.setBounds(30,180,400,40);
inputPathOfLogs.setText("g:\\BSMS\\backup\\logs\\BSMS.logs");
c.add(noticeLabel2);
noticeLabel2.setFont(new Font("华文新魏",Font.PLAIN, 18));
noticeLabel2.setBounds(50,220,330,40);
noticeLabel2.setForeground(Color.red);
//分界线
c.add(line2);
line2.setBounds(0,250,500,10);
line2.setForeground(Color.BLUE);
//恢复提交按钮
c.add(restoreButton);
restoreButton.setBounds(170,300,70,40);
restoreButton.addActionListener(this);
//清除按钮
c.add(clearButton);
clearButton.setBounds(250,300,70,40);
clearButton.addActionListener(this);
show();
}
//事件监听
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==restoreButton){
//st=connectDB.conDB();
flag=true;
String getPathOfDatabase=inputPathOfDatabase.getText().trim();
String getPathOfLogs=inputPathOfLogs.getText().trim();
if(getPathOfDatabase.equals("")==true&&getPathOfLogs.equals("")==true){
JOptionPane.showMessageDialog(null,"请输入存放备份路径再做恢复");
flag=false;
}
String command="restore database BSMS from disk='"+getPathOfDatabase+"' with norecovery ";
try{
st=connectDB.conDB("master");//数据库连接
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master","sa","5201314");
//st=connectDB.conDB("master");
if(flag){
st.executeUpdate(command);//从磁盘恢复数据库
JOptionPane.showMessageDialog(null,"数据库成功恢复");
}
}catch(SQLException ex){
if(ex!=null){
JOptionPane.showMessageDialog(null,ex);
flag=false;
}
}
command="restore log BSMS from disk='"+getPathOfLogs+"' with recovery";
try{
if(flag){
st.executeUpdate(command);//用日志恢复到时间点
JOptionPane.showMessageDialog(null,"恢复日志成功");
}
}catch(SQLException ex){
if(ex!=null){
JOptionPane.showMessageDialog(null,ex);
flag=false;
}
}
closeDB.closeDB(st,con);
}
else if(ae.getSource()==clearButton){
inputPathOfDatabase.setText("");
inputPathOfLogs.setText("");
flag=true;
}
}
public static void main(String args[]){
new restore();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -