Hope someone can help me with this!
I have a sql file that looks like this:
CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT UNIQUE (firstname,lastname)
)
ENGINE=InnoDB
;
INSERT IGNORE INTO users (firstname,lastname) VALUES ('x','y');
I created a web application that initializes the mysql database at startup using this function:
public static void initDatabase(ConnectionPool pool, File sqlFile){
Connection con = null;
Statement st = null;
String mySb=null;
try{
con = pool.getConnection();
mySb=IOUtils.copyToString(sqlFile);
String[] inst = mySb.split(";");
st = con.createStatement();
for(int i = 0; i<inst.length; i++){
if(!inst[i].trim().isEmpty()){
st.executeUpdate(inst[i]);
}
}
st.close();
}catch(IOException e){
throw new RuntimeException(e);
}catch(SQLException e){
throw new RuntimeException(e);
}finally{
SQLUtils.safeClose(st);
pool.close(con);
}
}
(This function was found on the Internet. The author, please forgive me for not saying your name, I lost it!)
It works fine if there are no SQL comment blocks.
A function copyToString()basically does what it says. Now I need to create a regular expression that will remove the block comments from the string. I have only block comments /* */in the file, no --.
What I have tried so far:
mySb = mySb.replaceAll("/\\*.*\\*/", "");
Unfortunately, I am not very good at regex ...
" /* comment */ real statement /* another comment*/" .....