Pessoal, boa tarde! Poderiam fazer a gentileza de me ajudar? Não estou conseguindo fazer a variável conn acessar a classe que conecta ao banco, podem me ajudar. Muito obrigado.
public class PesquisaArquivo2 {
// private JPanel jContentPane = null;
// private JLabel rotulo = null;
// private static Connection conn;
private static int i = 0;
private static String stringa1, stringa2, stringa3;
public static void main(String[] args) throws IOException, BiffException, ClassNotFoundException, SQLException{
//conecta ao banco de dados
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/alert_brasil","root","120797");
//Define dados do painel
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Alert Brasil");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
//Define botão para pesquisar/selecionar arquivo
JButton button = new JButton("Selecione o Arquivo: ");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//seleciona o arquivo
String caminhoArquivo = "";
JFileChooser arquivo = new JFileChooser();
int retorno = arquivo.showOpenDialog(null);
if(retorno == JFileChooser.APPROVE_OPTION){
caminhoArquivo = arquivo.getSelectedFile().getAbsolutePath();
Workbook workbook = Workbook.getWorkbook(new File(caminhoArquivo));
Sheet sheet = workbook.getSheet(0);
int linhas = sheet.getRows();
for(i = 0; i < linhas; i++){
Cell a1 = sheet.getCell(0,i);
Cell b2 = sheet.getCell(1,i);
Cell c3 = sheet.getCell(2,i);
stringa1 = a1.getContents();
stringa2 = b2.getContents();
stringa3 = c3.getContents();
//parte onde apresenta o erro no código
PreparedStatement ps = conn.prepareStatement("INSERT INTO agente(nome,prenome,sobrenome)" +
"VALUES('"+stringa1+"','"+stringa2+"','"+stringa3+"')");
ps.executeUpdate();
}//fim do for
}//else{
// }//fim do else
}
// }
//fim da conexao ao banco
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
}