Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. public void lerDocumento(String nome){
  2.        
  3.         Banco banco = new Banco();
  4.         Connection conexao = banco.getConnection();
  5.  
  6.         String valores[];
  7.        
  8.         try {
  9.             BufferedReader br = new BufferedReader(new FileReader(caminho + "/" + "AnexoI.csv"));//Indica local e nome do arquivo .csv
  10.             System.out.println(caminho);//Exibe local do arquivo .csv
  11.             String linha;
  12.  
  13.             br.readLine();
  14.             while ((linha = br.readLine()) != null) {//Enquanto a leitura das linhas não chegar em vazio...
  15.                 //valores = null;
  16.                 valores = linha.split(",");//O array recebe as linhas quebradas por ,
  17.                 banco.inserirDados(Integer.parseInt(valores[0]), valores[1] , valores[2], valores[3], Integer.parseInt(valores[4]), Integer.parseInt(valores[5]), Integer.parseInt(valores[6]));
  18.             }
  19.  
  20.             br.close();//Encerra a conexão com o buffer, ou seja, a leitura
  21.  
  22.         } catch (Exception e) {
  23.             System.out.println("Erro ao ler o arquivo! " + e);
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.  
  29.  
  30. public void inserirDados(int matricula, String nome, String curso, String disciplina, int turma, int ano, int semestre){
  31.         try {
  32.             String sql = "Insert into alunos(matricula, nome, curso, disciplina, "
  33.                     + "turma, ano, semestre) values(?, ?, ?, ?, ?, ?, ?)";
  34.             PreparedStatement state = getConnection().prepareStatement(sql);//Prepara o estado da conexão com o comando sql
  35.             state.setInt(1, matricula);//Seta os dados no bd
  36.             state.setString(2, nome);//Seta os dados no bd
  37.             state.setString(3, curso);//Seta os dados no bd
  38.             state.setString(4, disciplina);//Seta os dados no bd
  39.             state.setInt(5, turma);//Seta os dados no bd
  40.             state.setInt(6, ano);//Seta os dados no bd
  41.             state.setInt(7, semestre);//Seta os dados no bd
  42.  
  43.             state.execute();//Executa o insert
  44.             state.close();//Encerra o comando sql
  45.             close();//Fecha a tabela
  46.             System.out.println("Dados inseridos!");
  47.         } catch (SQLException ex) {
  48.             System.err.println("Erro ao inserir dados " + ex);
  49.         }
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement