What is the SQL statement to insert data in my Database in MySQL in Java?

0

In my program using Java in Eclipse and MySQL, I have the following data:

  • Call number (int): callcenter.getTotalCalls ()
  • Type of call (string): Local
  • Source Number (string): txtOrderNumber.getText ()
  • Destination Number (string): txtDefaultNumber.getText ()
  • Duration (int): Integer.parseInt (txtDuracion.getText ())

I am learning to use Databases and I am confused when inserting this data into my table.

As I read on the Internet, until now I have this but, it does not work, Eclipse marks it in red saying it is wrong:

String sql = "INSERT INTO llamadas (ID, Tipo, NumeroOrigen, NumeroDestino, Duracion, Precio) values ('"+ callcenter.getTotalLlamadas() +"', Local, '"txtNumeroDeOrigen.getText().replaceAll("\D+", "")"', '"txtNumeroDeDestino.getText().replaceAll("\D+", "")"', '"txtDuracion.getText()"', '"callLocal.calcularPrecio()"')";

What is the correct sentence to be able to send the data obtained through my fields JFormattedTextField ?, the only data that is fixed is the Type, which is "Local", this I do not ask for it.

Thanks and regards, any help is welcome.

    
asked by Robert Gomez 22.11.2016 в 04:45
source

1 answer

2

Try the following prepared statement, which is used to send SQL statements to the database:

String query = "INSERT INTO llamadas (ID, Tipo, NumeroOrigen, NumeroDestino, Duracion, Precio)"
        + " values (?, ?, ?, ?, ?, ?)";

PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, callcenter.getTotalLlamadas());
preparedStmt.setString (2, "Local");
preparedStmt.setString (3, txtNumeroDeOrigen.getText());
preparedStmt.setString (4, txtNumeroDeDestino.getText());
preparedStmt.setString (5, txtDuracion.getText());
preparedStmt.setInt (6, callLocal.calcularPrecio());

preparedStmt.execute();
    
answered by 22.11.2016 / 04:55
source