Java - Swingworker Se puede poner varios store procedure dentro de un doInBackground

 
Vista:
Imágen de perfil de Pool

Swingworker Se puede poner varios store procedure dentro de un doInBackground

Publicado por Pool (1 intervención) el 14/05/2018 03:17:50
Saludos a la comunidad tengo una duda vengo haciendo un formulario donde tiene un boton, una barra de progreso y un textArea, donde yo presiono el boton y en apariencia va avanzando la barra de progreso y en el textarea se va mostrando cada tarea que se va ejecutando, este es mi codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Barra;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.util.Random;
 
import Barra.conex;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class ProgressBarDemo extends JPanel implements ActionListener, PropertyChangeListener {
 
    private JProgressBar progressBar;
    private JButton startButton;
    private JTextArea taskOutput;
    private Task task;
 
 
    class Task extends SwingWorker<Void, Void> {
        conex cnn = new conex();
        PreparedStatement ps = null;
        /*
         * Main task. Executed in background thread.
         */
 
        String[][] names = {
                            {"SELECT * FROM HISTORICO_DEUDA", "3"},
                            {"SELECT * FROM HISTORICO_DEUDA", "100"}
                           };
 
        @Override
        public Void doInBackground() {
 
//            Random random = new Random();
//            int progress = 0;
            //Initialize progress property.
            setProgress(0);
 
//            while (progress < 100) {
//                try {
//                    Thread.sleep(random.nextInt(1000));
//                } catch (InterruptedException ignore) {
//                
//                }
//                progress += random.nextInt(10);
//                setProgress(Math.min(progress, 100));
 
               for(int i=0;i<=names.length-1;i++){
 
                        try {
                                String query = names[i][0];
                                ps = cnn.getConnection().prepareStatement(query);
                                ResultSet res = ps.executeQuery();
                                while(res.next()){
                                        System.out.println("HISTORICO_DEUDA "+i+" : -- "+res.getString("COD_CLIENTE"));
                                }
                                res.close();
                                cnn.desconectar();
                        } catch (SQLException e) {
                                JOptionPane.showMessageDialog(null, "Error, no se conecto");
                                System.out.println(e);
                        }
 
                       setProgress(Integer.parseInt(names[i][1]));
               }
 
 
                return null;
        }
 
        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
                Toolkit.getDefaultToolkit().beep();
                startButton.setEnabled(true);
                setCursor(null);
                taskOutput.append("Done!\n");
        }
    }
 
    public ProgressBarDemo() {
        super(new BorderLayout());
 
        //Create the demo's UI.
        startButton = new JButton("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
 
        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
 
        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5,5,5,5));
        taskOutput.setEditable(false);
 
        JPanel panel = new JPanel();
        panel.add(startButton);
        panel.add(progressBar);
 
        add(panel, BorderLayout.PAGE_START);
        add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
 
    }
 
    /**
     * Invoked when the user presses the start button.
     */
    public void actionPerformed(ActionEvent evt) {
        startButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }
 
    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
            taskOutput.append(String.format("Completed %d%% of task.\n", task.getProgress()));
        }
    }


    /**
     * Create the GUI and show it. As with all GUI code, this must run
     * on the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ProgressBarDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new ProgressBarDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


dentro de doInBackground estoy ejecutando 2 quieries pero mas adelante los remplazare por procedimientos almacenados , quisiera saber si es posible ya que solo se planta en el primer query.

Saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder