想要从文件Java中单独获取insert语句

时间:2014-08-21 05:19:24

标签: java regex

我有一个文本文件,如:

insert into table 
values (‘test’,
<xml> 
</xml>);
insert into table 
values (‘test’,
<xml> 
<xml>
<xml>
</xml>);
insert into table 
values (‘test’,
<xml> 
<xml>
<xml>
<xml>
<xml>
</xml>);

现在我希望我能在程序中单独读取每个insert(完整语句)命令并对表执行。

我怎么能这样做?任何正则表达式都会有帮助吗?

3 个答案:

答案 0 :(得分:1)

你的意思是这样的吗?:

 String str = "insert into table \n"
            + "values (‘test’,\n"
            + "<xml> \n"
            + "</xml>);\n"
            + "insert into table \n"
            + "values (‘test’,\n"
            + "<xml> \n"
            + "<xml>\n"
            + "<xml>\n"
            + "</xml>);\n"
            + "insert into table \n"
            + "values (‘test’,\n"
            + "<xml> \n"
            + "<xml>\n"
            + "<xml>\n"
            + "<xml>\n"
            + "<xml>\n"
            + "</xml>);";

    String[] commands = str.split(";");
    for(String command : commands)
    {
        System.out.println(command + ";");
    }

收率:

insert into table 
values (‘test’,
<xml> 
</xml>);

insert into table 
values (‘test’,
<xml> 
<xml>
<xml>
</xml>);

insert into table 
values (‘test’,
<xml> 
<xml>
<xml>
<xml>
<xml>
</xml>);

答案 1 :(得分:1)

将文件内容读入字符串。然后使用String.split();

 String s = "insert into table values (‘test’,<xml> </xml>); insert into table values             
 ( ‘test’,<xml>" 
 + "<xml><xml></xml>); insert into table values (‘test’,<xml> <xml><xml><xml><xml>    
 </xml>);";
 String[] test = s.split(";");
 for(int i = 0; i < test.length; i++) {
    System.out.println(test[i]);
 }

答案 2 :(得分:-1)

您可以使用JDBC进行插入。参考例如 http://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm