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

321
Vistas
Execution of a SQL query in Java?

I have a bit of a problem while the answer might be pretty clear. I'm trying to execute a parser program which converts data to a SQL Database but I can't execute a statement, which is pretty logical. Is there a way that this issue can be fixed? Thanks for the help =)

Maybe this makes it more clear:

public int parser(String a, float b2, float c2) {
    int updated = 0;
    Connection conn = null;
    PreparedStatement stmt = null;
    try{

        conn = DriverManager.getConnection(url, user, password);

    // get a statement
        String insertSQL = "INSERT INTO testparser(garagenaam, xpos, ypos) VALUES(" + a + "," + b2 + "," + c2 +")";
        stmt = conn.prepareStatement(insertSQL);

        stmt.setString(1, a);
        stmt.setFloat(2, b2);
        stmt.setFloat(3, c2);

        updated = stmt.executeUpdate();

        System.out.println("Inserted data into the database...");


        } catch (SQLException se) {
        se.printStackTrace();
        } catch (Exception e) {
        e.printStackTrace();
        } finally {
        try {
            if (stmt != null)
                conn.close();
        } catch (SQLException se) {
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
    System.out.println("Thank you for your service.");
    this.conn = conn;
    return updated;

}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Aside from the weirdness with the conn variable the others have mentioned -- You can do the insert for all column in one update statement. And you should use a PreparedStatement for this to avoid SQL problems or Injection.

public int parser(String a, float b2, float c2) {
         // because I don't know where the conn is coming from
         conn = getConnection(); 

         // get a statement 
String insertSQL = "INSERT INTO testparser(garagename, xpos, ypos) VALUES(?,?,?)";
         PreparedStatement stmt = conn.prepareStatement(insertSQL);

          stmt.setString(1, a);
          stmt.setFloat(2, b2);
          stmt.setFloat(3, c2);

          int updated = stmt.executeUpdate();

          System.out.println("Inserted data into the database...");

... imagine try/catch/finally with closing of stuff as appropriate :)

          return updated;
      }
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