Advertisement
SergioArmando

Untitled

Jun 4th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package rellnabd;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileInputStream;
  10. import java.io.FileReader;
  11. import java.io.InputStreamReader;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.sql.Statement;
  18.  
  19. /**
  20. *
  21. * @author keko
  22. */
  23. public class RellnaBd {
  24.  
  25. /**
  26. * @param args the command line arguments
  27. */
  28. public static void main(String[] args) {
  29. // TODO code application logic here
  30. String contenido = "F://colo.txt";
  31. try
  32. {
  33. //ruta puede ser de tipo String o tipo File
  34. String ruta = "F://CapturaDeVoto.txt";
  35. FileReader fr = new FileReader( ruta );
  36. BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(ruta), "ISO-8859-1") );
  37.  
  38. String linea;
  39. //Obtenemos el contenido del archivo linea por linea
  40. while( ( linea = br.readLine() ) != null ){
  41. //System.out.println(linea);
  42.  
  43. String diaArray[] = linea.split(",");
  44.  
  45. int Afected;
  46. try {
  47. Class.forName("com.mysql.jdbc.Driver");
  48. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistemamonitoreo","root", "1234"); // OJO esta linea depende de tu base de datos, el 1234 es la contrasenia
  49. Statement stat = con.createStatement();
  50. String SQL = "INSERT INTO aspiranteresponsable (Nombre,) VALUES ('" + diaArray[0] + "','" + diaArray[2] + "','" + diaArray[1] + "','" + diaArray[4] + "')";
  51. System.out.println(" "+diaArray[0]+ " "+ diaArray[1]+ " " + diaArray[2] + " " + diaArray[3]+ " " + diaArray[4]);
  52. System.out.println(SQL);
  53.  
  54. PreparedStatement preparedStmt = con.prepareStatement(SQL,new String[]{"idcolonias"});
  55. //preparedStmt.executeUpdate();
  56. ResultSet rs = preparedStmt.getGeneratedKeys();
  57.  
  58. if (rs.next()) {
  59. Afected = rs.getInt(1);
  60. // System.out.println(Afected);
  61. }
  62. }catch ( ClassNotFoundException | SQLException e ){
  63. System.out.println("Error: " + e.getMessage());
  64.  
  65. }
  66. }
  67.  
  68. }catch( Exception e ){
  69. System.out.println("No se pudo abrir: " + e);
  70. }
  71. //finally se utiliza para que si todo ocurre correctamente o si ocurre
  72. //algun error se cierre el archivo que anteriormente abrimos
  73.  
  74.  
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement