Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!