1
C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más / Java SQL leer un fichero txt e introducir valores en tabla de MySql Connection
« en: 28 de Octubre 2020, 21:07 »
Buenas me piden lo siguiente: Necesito realizar un programa que me lea un fichero llamado “personas.txt” y introducir estos valores en la tabla clientes de mySql.
El fichero personas.txt tiene varios INSERT INTO CLIENTES de id, nombre y apellidos.
Yo tengo realizado lo siguiente pero no me funciona:
El fichero personas.txt tiene varios INSERT INTO CLIENTES de id, nombre y apellidos.
Yo tengo realizado lo siguiente pero no me funciona:
Código: [Seleccionar]
public class Clientes{
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
String nombredriver ="com.mysql.cj.jdbc.Driver";
ResultSet rs = null;
int ID = 0;
String NOMBRE = " ";
String Apellidos= " ";
String[] tokens;
FileReader file1 = new FileReader("src/personas.txt");
BufferedReader buffer1 = new BufferedReader(file1);
String line = buffer1.readLine();
Class.forName(nombredriver);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/clientes" +
"?useUnicode=true"+
"&serverTimezone=UTC", "root", "root");
Statement stmt=con.createStatement();
while((line = buffer1.readLine()) != null)
{
tokens = line.split(",");
ID = Integer.parseInt(tokens[0]);
NOMBRE = tokens[1];
APELLIDOS= tokens[2];
String insertString = "insert into clientes (ID, NOMBRE, APELLIDOS) values ('"+ID+"', '"+NOMBRE+"', '"+APELLIDOS+"'))";
stmt.executeUpdate(insertString);
}
con.close();
stmt.close();
}
}