博主地址:https://www.iteye.com/blog/53873039oycg-2018780#comments 已修复其中小bug
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Sql语句处理 {
public static void main(String[] args) throws Exception {
Sql语句处理 t = new Sql语句处理();
String content = new String(t.processSqlContent(
"f:/saveFile/tmp/c/testdb.sql", "utf-8"));
System.out.println(content);
t.writeStrToFile(content,"f:/saveFile/tmp/c/testdb.sql", "utf-8");
}
public String processSqlContent(String filePath, String charSet)
throws Exception {
if (charSet == null) {
charSet = "utf-8";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), charSet));
StringBuffer contentBuffer = new StringBuffer(5120);
String temp = null;
String tmpResult = null;
boolean isStart = false;
while ((temp = br.readLine()) != null) {
//去空格和;
tmpResult = new String(temp.replaceAll(";", " ").replaceAll("\\s{2,}", " "));
if (tmpResult != null) {
//去除同一行/* */注释
if(tmpResult.indexOf("/*")!=-1&&tmpResult.indexOf("*/")!=-1)
{
//最小匹配
tmpResult=tmpResult.replaceAll("\\/\\*.*?\\*\\/", "");
}else if (tmpResult.indexOf("/*") != -1&&tmpResult.indexOf("*/") == -1&&tmpResult.indexOf("--")==-1) {
// /*开始
isStart = true;
tmpResult = tmpResult.replaceAll("\\/\\*.*", "");
contentBuffer.append(tmpResult).append(System.lineSeparator());
}else if(tmpResult.indexOf("/*")!=-1&&tmpResult.indexOf("--")!=-1&&tmpResult.indexOf("--")<tmpResult.indexOf("/*"))
{
//同时存在--/*
tmpResult=tmpResult.replaceAll("--.*", "");
}
if (isStart && tmpResult.indexOf("*/") != -1) {
// */结束
isStart = false;
continue;
}
// 去除同一行的--注释
tmpResult = new String(tmpResult.replaceAll("--.*", ""));
}
if (!isStart) {
//保留换行符
//contentBuffer.append(tmpResult).append("\r\n");
//无换行符
contentBuffer.append(tmpResult);
}
}
temp = contentBuffer.toString();
//保留换行符
//temp = new String(temp.replaceAll("\\s{2,}\\r\\n"," "));
//无换行符
temp = new String(temp.replaceAll("\\s{2,}", " "));
return temp;
}
public String getFileContent(String fileName, String chartSet)
throws Exception {
if (chartSet == null) {
chartSet = "utf-8";
}
StringBuffer buffer = new StringBuffer(5120);
String line = null;
InputStream is = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
chartSet));
while ((line = reader.readLine()) != null) {
buffer.append(line).append("\r\n");
}
return buffer.toString();
}
public void writeStrToFile(String str, String filePath, String charsetName)
throws Exception {
if (charsetName == null) {
charsetName = "utf-8";
}
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
filePath), charsetName);
out.write(str);
out.close();
}
}
SQL测试语句
select
--注释/*注释*/
* --去全部
/*注释*/
/*测试--*/
from emp --从emp取数据/**测试
/*
测试注释
--注释*/
/*--测试*/
/*多行注释*/
/**2B注释*/
/* *
*取dept=10 日期小于1998的数据
*/
/*select *
from emp
where 1 = 1
and deptno = 12
--and hiredate < to_date('2013-10-01', 'yyyy-mm-dd')*/
where /**/
1 = /*注释--这也是注释*/
/*注释*/
1 --1=1
and deptno = /*--*/ /*注释---*/
10 --部门
/*
注释//嵌套注释*/
and /*时间字段*/
hiredate /*测试注释/*测试注释*/
< /*日期小于1998*/
to_date('1998-10-01', 'yyyy-mm-dd' /*年月日格式*/)