通过Java程序备份MS SQL Server数据库?

时间:2016-03-18 12:52:47

标签: java sql-server

我的问题很简单。我有可能按照这个过程完成。

像这样的命令

backup database master to disk='\\csd0201wnt29f\backup\master.bak' with stats=1

我的意思是stats=1条款。

方法

 public void executar(Backup bkp, String comando) throws Exception {
        Connection conexao = ConnectionFactory.connectBase(bkp.getServidor(), bkp.getUsuario(), bkp.getSenha());
        PreparedStatement cmd = conexao.prepareCall(comando, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        PreparedStatement cmdQuery = conexao.prepareCall("select @@spid");
        ResultSet set = cmdQuery.executeQuery();
        int spid=0;
        if (set.next()) {
            spid=set.getInt(1);
            System.out.println("SPID" + set.getInt(1));
        }
        SQLMonitor s = new SQLMonitor(bkp, spid);
        s.start();
        cmd.execute();        

        cmdQuery.close();        
        cmd.close();
        conexao.close();
    }

电话

 public void RealizaBackup(Backup bkp) throws Exception {
        SQLComando cmd = new SQLComando();

        System.out.println("backup database " + bkp.getBase() + " to disk='" + bkp.getCaminho() + "\\" + bkp.getBase() + ".bak' with stats=1");
        cmd.executar(bkp, "backup database " + bkp.getBase() + " to disk='" + bkp.getCaminho() + "\\" + bkp.getBase() + ".bak' with stats=1");
    }

0 个答案:

没有答案