Problemas no restore mysql via JAVA

1 resposta
fabricioknight

fala galera, to com um problema aqui, eu criei uma opção de backup e restore no sistema que estou desenvolvendo, o backup ta funcionando perfeitamente mas o restore...

o codigo do restore é esse aqui

"
        javax.swing.JFileChooser jfc = new javax.swing.JFileChooser();
        jfc.setMultiSelectionEnabled(false);
        jfc.setDialogTitle("Selecione um arquivo sql");
        jfc.setFileFilter(new FileNameExtensionFilter("SQL", "sql"));
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int resposta = jfc.showOpenDialog(this);


        if (resposta == 0) {

        try{
        String comando = "cmd /c mysql -uroot -proot -f clinica < " +jfc.getSelectedFile();
        Runtime.getRuntime().exec(comando);
        JOptionPane.showMessageDialog(null, "Arquivo restaurado com sucesso!");
        }catch(Exception e){
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"Falha ao restaurar arquivo!");
         }

        }

so que ele nao ta restaurando, tipo se eu colocar c://banco.sql no lugar de +jfc.getSelectedFile(); ele restaura normalmente, mas eu coloquei esse getSelectedFile pro usuario pode procurar o arquivo a ser restaurado atraves de um JFileChooser, mas nao funciona

se alguem puder me dar uma luz ficarei grato :D

1 Resposta

Cleber_Carvalho

[quote=Cleber Carvalho]
Também com o mesmo problema se alguém sabe como resolver nos ajude!!!
Oi, estava lendo um tópico que me tirou muitas dúvidas sobre Restauração de Backup Mysql.
A partir de exemplos encontrados na net, elaborei um procedimento para restaurar Backup. Até ai beleza.
O problema é que depois de selecionar o arquivo e executar o código aparentemente entra em lupe ou sei lá trava, já li e re li o código, mas não encontro o erro.
Peço encarecidamente sua ajuda de uma olhada e diga o que acha:

private void JB_BT_Restaurar_MysqlActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        // TODO add your handling code here:
        try {
            JFC_Backup.setVisible(true);
            String bd = &quot;sisdimapol&quot;;
            int result = JFC_Backup.showOpenDialog(null);

            if (result == JFileChooser.OPEN_DIALOG) {

                File bkp;
                bkp = JFC_Backup.getSelectedFile();
                String arq = bkp.getPath();
                System.out.println(&quot;bd &quot; + bd);
                System.out.println(&quot;arq &quot; + arq);
                String[] cmd = new String[3];
                cmd[0] = &quot;cmd.exe&quot;;
                cmd[1] = &quot;/C&quot;;
                //cmd[2] = &quot;c:\\xampp\\mysql\\bin\\mysql -u root -qwe123 -h localhost &quot; + bd + &quot; &lt; &quot; + arq;
                cmd[2] = &quot;C:\\wamp\\bin\\mysql\\mysql5.5.8\\bin\\mysql -u root -p qwe123 -h localhost &quot; + bd + &quot; &lt; &quot; + arq;

                Runtime rt = Runtime.getRuntime();
                System.out.println(&quot;Execing &quot; + cmd[0] + &quot; &quot; + cmd[1]);
                proc = rt.exec(cmd);

                // any error message?

                StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), &quot;ERROR&quot;);

                // any output?
                StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), &quot;OUTPUT&quot;);

                // kick them off
                errorGobbler.run();
                outputGobbler.run();

                // any error???
                int exitVal = proc.waitFor();
                if (exitVal == 0) {
                    JOptionPane.showMessageDialog(null, &quot;Backup Restaurado com sucesso !&quot;);
                } else {
                    JOptionPane.showMessageDialog(null, &quot;Falha ao restaurar backup. \n Verifique as configurações ou entre em contato com o suporte !&quot;);
                }
            }


        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, &quot;Erro!&quot;, 2);
        }
Esta classe também é necessária para execução do código acima, há estou utilizando o xampp
public class StreamGobbler {
    
 
    InputStream is;
    String type;
    OutputStream os;
    
    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }
    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }
    
    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {            
                if (pw != null)
                    pw.println(line);
                System.out.println(type + &quot;&gt;&quot; + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
    }


    
}
Criado 22 de julho de 2010
Ultima resposta 6 de set. de 2012
Respostas 1
Participantes 2