Note that there are some explanatory texts on larger screens.

plurals
  1. POJava regex to remove SQL comments from a string
    primarykey
    data
    text
    <p>Hope someone can help me out with this one !</p> <p>I have a sql file that looks like this:</p> <pre><code>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'); /* INSERT IGNORE INTO users (firstname,lastname) VALUES ('a','b'); */ </code></pre> <p>I have buit a web application that initializes a mysql database at startup with this function:</p> <pre><code>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); // We use ";" as a delimiter for each request then we are sure to have well formed statements String[] inst = mySb.split(";"); st = con.createStatement(); for(int i = 0; i&lt;inst.length; i++){ // we ensure that there is no spaces before or after the request string // in order not to execute empty statements 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); } } </code></pre> <p>(This function was found on the web. Author, please forgive me for not citing your name, I lost it !!)</p> <p>It works perfectly as long as there is not SQL comment blocks.</p> <p>The <code>copyToString()</code> function basically does what it says. What I would like now is build a regex that will remove block comments from the string. I only have block comments <code>/* */</code> in the file, no <code>--</code>. </p> <p>What I have tried so far:</p> <pre><code>mySb = mySb.replaceAll("/\\*.*\\*/", ""); </code></pre> <p>Unfortunatly, I'm not very good at regex...</p> <p>I get all the troubles of "The matched string look something like <code>/* comment */ real statement /* another comment*/</code> " and so on...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload