Add records to the database with JAVA and batch

1

I have the following code in a class called BD ,

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.*;
import java.util.List;
import java.util.ArrayList;
public class BD {

private String driver = "com.mysql.jdbc.Driver";
private String cadenaConexion="jdbc:mysql://127.0.0.1/log";
private String usuario="root";
private String contraseña="";

public BD(){
    try {
        Class.forName(driver);//cargar el driver que se ha creado 
        con=DriverManager.getConnection(cadenaConexion,usuario,contraseña);
        System.out.println("Conectado con BD");
    }catch(Exception e){
        System.out.println("No se ha podido establercer una conexion");

    }   

}

private PreparedStatement psInsertar;
private Statement st;
private Connection con;

public void insertar (List<Datos> lista) throws SQLException {

    st=con.createStatement();
    String sql = "INSERT INTO  LOG_PG509 (fecha,hora,milisegundo,secuencia,banco,suscriptor,codrespmov,tiempo_total,tiempo_pic,tiempo_movilnet,servicio,detalle_excepcion)"+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
    psInsertar = con.prepareStatement(sql);
    for(Datos d : lista){
        psInsertar.setString(1, d.getfecha());
        psInsertar.setString(2, d.gethora());
        psInsertar.setString(3, d.getmilisegundo());
        psInsertar.setString(4, d.getsecuencia());
        psInsertar.setString(5, d.getbanco());
        psInsertar.setString(6, d.getsuscriptor());
        psInsertar.setString(7, d.getcodrespmov());
        psInsertar.setString(8, d.gettiempo_total());
        psInsertar.setString(9, d.gettiempo_pic());
        psInsertar.setString(10, d.gettiempo_movilnet());
        psInsertar.setString(11, d.getservicio());
        psInsertar.setString(12, d.getdetalle_excepcion());

        psInsertar.addBatch();
    }
psInsertar.executeBatch();
}
}

And in another class called Prueba I need to call the list created in the previous class BD.

I show you the Prueba code:

Datos d1 =new Datos(fecha,hora,milisegundo,secuencia,banco,suscriptor,codrespmov,tiempo_total,tiempo_pic,tiempo_movilnet,servicio,detalle_excepcion);
BD bd = new BD();
bd.insertar(d1);

There are many records and the intention is to add them all to the Database with batch

    
asked by Javier Valero 06.09.2018 в 15:16
source

0 answers