Boa tarde;
Fiz um frame para importar uma planilha excel e salvar dentro de banco de dados access.
Isto está funcionando perfeitamente.
O que eu quero é que me mostre em um label, durante a importação, o % que está o processo.
Não estou recebendo nenhum erro, mas o numero do % não aparece.
O que estou fazendo de errado.
O problema esta na linha 149 à 152
Segue o codigo.
package telas;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utilitarios.Conexao;
public class Importacao extends javax.swing.JFrame {
Conexao rs;
public Importacao() {
initComponents();
setLocationRelativeTo(null);
rs = new Conexao();
rs.conecta();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
edtArquivo = new javax.swing.JTextField();
btnLocalizar = new javax.swing.JButton();
btnImportar = new javax.swing.JButton();
LBarra = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
menuFechar = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Import of temperatures");
jLabel2.setText("Arquivo:");
btnLocalizar.setText("Localizar");
btnLocalizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLocalizarActionPerformed(evt);
}
});
btnImportar.setText("Importar");
btnImportar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportarActionPerformed(evt);
}
});
LBarra.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
LBarra.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
LBarra.setText("0 %");
menuFechar.setText("Close");
menuFechar.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
menuFecharMouseClicked(evt);
}
});
jMenuBar1.add(menuFechar);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(edtArquivo, javax.swing.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE)
.addGap(18, 18, 18))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(LBarra, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(270, 270, 270)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnImportar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnLocalizar, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(30, 30, 30))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(edtArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnLocalizar))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(btnImportar))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(LBarra, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void menuFecharMouseClicked(java.awt.event.MouseEvent evt) {
rs.desconecta();
dispose();
}
private void btnImportarActionPerformed(java.awt.event.ActionEvent evt) {
String plotBid="";
String Sql="";
if (edtArquivo.getText().equals(""))
JOptionPane.showMessageDialog(null, "Informe o arquivo a ser importado!");
else {
try {
Workbook book = Workbook.getWorkbook(new File(edtArquivo.getText()));
Sheet sheet = book.getSheet(0);
int totlin = sheet.getRows();
int totcol = sheet.getColumns();
int perc = 0;
for (int lin = 1; lin < totlin; lin++) {
perc = lin * 100 / totlin;
LBarra.setText(String.valueOf(perc) + " %");
LBarra.revalidate();
LBarra.repaint();
this.repaint();
plotBid = sheet.getCell(0,lin).getContents().toString();
rs.executeSQL("Select * from TRegistros Where [Plot BID]='"+plotBid+"'");
if (!rs.resultset.next()) {
rs.statement.executeUpdate("Insert Into TRegistros ([Plot Bid]) Values ('"+plotBid+"')");
}
for (int col = 1; col < totcol; col++) {
String var = sheet.getCell(col, 0).getContents().toString();
String cell = sheet.getCell(col,lin).getContents().toString();
if (!cell.equals("")) {
if (var.equals("Season") || var.equals("Set") || var.equals("TrackID") ||
var.equals("TrialType") || var.equals("Group 1") || var.equals("Group 2") ||
var.equals("Group 3") || var.equals("COMM")) {
Sql = "Update TRegistros Set [" + var+"]='"+cell+
"' Where [Plot Bid]='" + plotBid + "'";
rs.statement.executeUpdate(Sql);
} else {
if (var.equals("Entry#") || var.equals("Rep#") ||
var.equals("Row Width") || var.equals("Column Width")) {
Sql = "Update TRegistros Set [" + var+"]="+cell+
" Where [Plot Bid]='"+plotBid+"'";
rs.statement.executeUpdate(Sql);
} else {
if (var.equals("Planting Date")) {
Sql = "Update TRegistros Set [" + var+"]=#"+cell+
"# Where [Plot Bid]='"+plotBid+"'";
rs.statement.executeUpdate(Sql);
} else {
if (var.equals("Pedigree")) {
String manf=cell;
rs.executeSQL("Select * From TRegistro_Parentais Where Breeding_Code='"+cell+"'");
if (rs.resultset.next()) {
if (!rs.resultset.getString("Nome_Manufatura").equals(""))
manf = rs.resultset.getString("Nome_Manufatura");
else {
if (!rs.resultset.getString("Nome_Pre_Comercial").equals(""))
manf = rs.resultset.getString("Nome_Pre_Comercial");
}
}
rs.executeSQL("Select * from TRegistros");
Sql = "Update TRegistros Set [" + var+"]='"+cell+
"', [Manuf]='" + manf + "' Where [Plot Bid]='"+plotBid+"'";
rs.statement.executeUpdate(Sql);
} else {
rs.executeSQL("Select * From TVariaveis Where Variavel='"+var+"'");
if (rs.resultset.next()) {
rs.executeSQL("Select * from TDados Where [Plot Bid]='"+plotBid+
"' and Variavel='"+var+"'");
if (!rs.resultset.next()) {
Sql= "Insert Into TDados ([Plot Bid], Variavel, Conteudo) "+
"Values ('"+plotBid+"', '"+var+"', '"+cell+"')";
rs.statement.executeUpdate(Sql);
} else {
Sql = "Update TDados Set Conteudo='"+cell+"' "+
"Where [Plot Bid]='"+plotBid+"' and Variavel='"+var+"'";
rs.statement.executeUpdate(Sql);
}
}
}
}
}
}
}
}
}
menuFecharMouseClicked(null);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro no BD\n"+ex);
} catch (IOException ex) {
Logger.getLogger(Importacao.class.getName()).log(Level.SEVERE, null, ex);
} catch (BiffException ex) {
Logger.getLogger(Importacao.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void btnLocalizarActionPerformed(java.awt.event.ActionEvent evt) {
JFrame frame=null;
String filename = File.separator+"xls";
JFileChooser fc = new JFileChooser(new File(filename));
fc.showOpenDialog(frame);
edtArquivo.setText(fc.getSelectedFile().toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Importacao().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel LBarra;
private javax.swing.JButton btnImportar;
private javax.swing.JButton btnLocalizar;
private javax.swing.JTextField edtArquivo;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenu menuFechar;
// End of variables declaration
}
Obrigado.