Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
How to insert a scanned value on a mySQL table?

I have an issue on how to insert a value from another table into a new one. I have the following code:

Scanner scanner1 = new Scanner(System.in);
java.sql.Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM flight");    
int f_id = scanner.nextInt();  
int air_id = scanner.nextInt();
          while (res.next()) {
             if (Integer.parseInt(res.getString("air_id")) == air_id && Integer.parseInt(res.getString("f_id")) == f_id) {
                if (Integer.parseInt(res.getString("f_status_f_id_s")) > 0) {
                      System.out.println("You can reserve");
                      stmt=(Statement) conn.createStatement();
                      stmt.execute("SET FOREIGN_KEY_CHECKS=0");
                            String query1= "INSERT INTO reservations" +" VALUES(1,6/6/2020,)"+air_id+"(..,..,;
                            stmt.executeUpdate(query1);

To be more specific on the line String query1= "INSERT INTO reservations" +" VALUES(1,6/6/2020,)"+air_id+"(..,..,; I want the air_id to be the value I scanned before such as f_id. Is this the right syntax way to do it?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can insert the variable in your query by using the following code:

String query1= "INSERT INTO reservations" +" VALUES(1,6/6/2020,)"+air_id=@air_id+"(..,..,;

Thank you, I hope it helps you

over 4 years ago · Santiago Trujillo Denunciar

0

Use Prepared Statements to that .i.e:

 PreparedStatement stmt= con.prepareStatement();
 stmt.execute("SET FOREIGN_KEY_CHECKS=0");
  //put ?(no of values you need to insert)
  String query1= "INSERT INTO reservations values(?,?,?)";
  stmt.setInt(1,1);//1 specifies the first parameter in the query  
  stmt.setString(2,"6/6/2020"); 
  stmt.setInt(3,air_id);//pass your variable
  stmt.executeUpdate(query1);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda