【发布时间】:2012-04-19 10:11:12
【问题描述】:
希望有人能帮我解决这个问题!
我有一个如下所示的 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() 函数基本上按照它说的做。
我现在想要的是构建一个正则表达式,它将从字符串中删除块 cmets。我在文件中只有块 cmets /* */,没有 --。
到目前为止我所尝试的:
mySb = mySb.replaceAll("/\\*.*\\*/", "");
不幸的是,我不太擅长正则表达式...
我遇到了“匹配的字符串看起来像/* comment */ real statement /* another comment*/”等等的所有麻烦......
【问题讨论】:
-
你的正则表达式中需要惰性运算符
?