简体   繁体   中英

Conditional SQL queries and JDBC

I'm writing a program utilizing JDBC that will check if a table exists and create it if it doesn't.

I was planning to include the following:

String query = (some query);
int createIfNotExists = connection.createStatement().executeUpdate(query);

But it's not allowing me to use "IF" in my SQL query. Why is this? Do I need to use some different type of driver? Or is IF just not allowed with JDBC? Has anyone dealt with this before, and how'd you handle it?

DatabaseMetaData md = connection.getMetaData();  
String query = "";
boolean exist=false  

ResultSet rs = md.getTables(null, null, "table_name", null);

while (rs.next()) {
  exist = True ;
}  

if(!exist){
       query="CREATE TABLE table_name ...";
       connection.createStatement().executeUpdate(query);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM