希望有人可以帮我解决这个问题!
我有一个看起来像这样的sql文件:
我已经建立了一个Web应用程序,它通过以下函数在启动时初始化MySQL数据库:
我有一个看起来像这样的sql文件:
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');
*/
我已经建立了一个Web应用程序,它通过以下函数在启动时初始化MySQL数据库:
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<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);
}
}
这个函数是从网络上找到的。作者请原谅我没有引用你的名字,我忘了!!
只要没有SQL注释块,它就可以完美地工作。
copyToString()
函数基本上做它所说的事情。
现在我想构建一个正则表达式来从字符串中删除块注释。文件中只有块注释/* */
,没有--
。
我目前尝试过的:
mySb = mySb.replaceAll("/\\*.*\\*/", "");
很遗憾,我不是很擅长正则表达式...
我遇到了所有与“匹配的字符串看起来像/*注释*/真实语句/*另一个注释*/
”等相似的问题...
?
。 - Gabber