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