I have a method called dropTable
that sends a SQL query to a database to check whether a table exists or not. If it does, it drops the table. I send another query telling it to create a table.
When I run the code, I get an error saying
ERROR: syntax error at or near "IF" Position: 1` that points to the line with the first execute method.
I have print statements to see where to code reaches and it never reaches past the execute method in question.
public static void dropTable(Connection conn, String name, String description) {
Statement st = null;
try {
st = conn.createStatement();
System.out.println("hello");
st.execute("IF EXISTS (SELECT * FROM sys.tables WHERE NAME = '" + name + "' AND TYPE = 'U') DROP TABLE " + name);
System.out.println("done");
st.execute("CREATE TABLE " + description);
} catch (SQLException e) {
e.printStackTrace();
}
}
Am I doing something wrong?