Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda