0
点赞
收藏
分享

微信扫一扫

Oracle中SQL语句转换为Java字符串拼接的形式 -- Java


对于很长的SQL, Java中查询数据使用字符串拼接的形式, 如果手动去拼接将十分耗时, 通过下面Java工具类, 可以自动在每行两侧添加双引号, 最后添加分号, 并且SQL中存在注释的位置​​--​​​ 将会使用Java​​//​​注释掉, 这样只要专心写好sql, 在变量的位置手动修改一下就可以直接粘贴到代码中.

package cn.com.clearlight.JavaUtils

import java.io.*;

/**
* 寫好的 SQL要站到代碼中, 每行需要添加引號, 該類可以直接給每行開頭和末尾去除空白字符後添加引號, 處第一行開頭還會添加 + , 字符串末尾添加 ; ,
* 經過該類處理, 可以直接將生成的sql 粘貼到代碼中
* <p>
* 示例:
* -- 第一張報表sql
* with node as( -- sql注釋
* select wage.Agent_Effective_Type(a.agentcode, f.ym) packagetype,
* <p>
* 處理後:
* // -- 第一張報表sql
* " with node as( " // -- sql注釋
* + "select wage.Agent_Effective_Type(a.agentcode, f.ym) as packagetype, "
* ;
*
* @author xyLi
* @Time 2020-12-29
*/
public class SqlToStringUtils {

/**
* 獲取文件夾的路徑, 可以為文件夾路徑或文件路徑
* 如果為文件夾路徑, 則會對文件夾中的所有文件進行批量轉換
* @param path 文件或文件夾路徑
* @param isDelLineSpace 轉換的代碼是否保留sql中每行的縮進 -> 0:保留, 1:删除行首空白字符
*/
public static void getFilePath(String path, int isDelLineSpace) {
File file = new File(path);
// 如果路徑為文件夾
if (file.isDirectory()) {
// 獲取路徑中的所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 處理獲取的每個文件
readTxtFile(files[i], isDelLineSpace);
}
} else {
readTxtFile(new File(path), isDelLineSpace);
}
}

/**
* 将sql转换为java代码中拼接字符串后的内容
*
* @param sqlFile SQL源文件
* @param isDelLineSpace 轉換的代碼是否保留sql中每行的縮進 -> 0:保留, 1:删除行首空白字符
*/
public static void readTxtFile(File sqlFile, int isDelLineSpace) {
try {
// File sqlFile = new File(filePath);
// 獲取sql文件名
System.out.println(sqlFile.getName());
String[] sqlName = sqlFile.getName().split("\\.");
String path = "E:\\SqlToStringUtil\\Export\\" + sqlName[0].toString() + "_str.txt";
File stringFile = new File(path);
if (!stringFile.getParentFile().exists()) {
try {
stringFile.getParentFile().mkdirs(); // 不存在則創建父目錄
stringFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 如果文件不存在, 則自動創建
if (!stringFile.exists()) {
stringFile.createNewFile();
}
FileOutputStream outputStream;
outputStream = new FileOutputStream(stringFile);
StringBuilder stringBuilder = new StringBuilder();
if (sqlFile.isFile() && sqlFile.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(sqlFile), "UTF-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
int lineNum = 1;
while ((lineTxt = br.readLine()) != null) {
// 刪除文本空行
if ("".equals(lineTxt.trim())) {
continue;
}
// 刪除每行的開始和結尾的空行
if (isDelLineSpace == 1) {
lineTxt = lineTxt.trim();
}
// 若該行為注釋, 則添加//
if (lineTxt.trim().startsWith("--")) {
stringBuilder.append("// " + lineTxt + " " + " \n ");
continue;
} else {
// 該行若包含--,有注釋則在--之前 添加//提前注釋掉
int charIndex = lineTxt.indexOf("--");
if (charIndex == -1) { // 不含 --
if (lineNum == 1) { // 首行的處理, 不添加 +
stringBuilder.append(" \" " + lineTxt + " \" " + " \n ");
lineNum = -1;
continue;
}
// System.out.println(lineTxt);
stringBuilder.append("+ \"" + lineTxt + " \" " + " \n ");
} else {
if (lineNum == 1) {
stringBuilder.append(" \" " + lineTxt.replace("--", " \" // --") + " \n ");
lineNum = -1;
continue;
}
// System.out.println(lineTxt);
stringBuilder.append("+ \"" + lineTxt.replace("--", " \" // --") + " \n ");
}
}
}
// 字符串結尾添加分號
stringBuilder.append(";");
// 將字符串寫入文件中
String context = stringBuilder.toString();
byte[] bytes = context.getBytes("UTF-8");
outputStream.write(bytes);
br.close();
outputStream.close();
System.out.println("文本轉換成功!");
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文本轉換出錯!");
e.printStackTrace();
}
}

// 調用方法getFilePath
public static void main(String[] args) {
// 批量轉換 則傳入文件目錄, 單文件轉換 則傳入文件絕對路徑
// 若輸出文本中文亂碼, 需將要导出的文本另存為 UTF-8形式的文本
//getFilePath("E:\\SqlToStringUtil\\origin\\I_603918_20.sql", 1);
getFilePath("E:\\SqlToStringUtil\\origin", 0);
}

}

其实上面代码并不好, 对于字符串拼接的形式是不如StringBuilder进行append节省资源的. 还有很大优化空间.


举报

相关推荐

0 条评论