码迷,mamicode.com
首页 > 数据库 > 详细

字符串转换:sql

时间:2015-09-11 19:28:46      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ConvertToString {
	public static Pattern sqlPatern = null;
	static {
		String regex = "\\$P\\{(\\w+|\\_)\\}";
		sqlPatern = Pattern.compile(regex);
	}
	public static void main(String args[]) {
		convertSQLToJavaString();

	}
	private static void convertSQLToJavaString() {
		String readPath = "C:\\Users\\Administrator\\Desktop\\sql.txt";
		String writePath = "C:\\Users\\Administrator\\Desktop\\out.txt";
		File readFile = new File(readPath);
		File writeFile = new File(writePath);
		try {
			writeFile.createNewFile();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader(readFile));
			writer = new BufferedWriter(new FileWriter(writeFile));
			String line = null;
			while ((line = reader.readLine()) != null) {
				line = "\" " + line +" \",";
				writer.write(replaceParameters(line));
				writer.newLine();
			}
			System.out.print("success");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				reader.close();
				writer.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		} finally {
			try {
				writer.flush();
				reader.close();
				writer.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	private static String replaceParameters(String line) {
		Matcher m = sqlPatern.matcher(line);
		StringBuffer sb = new StringBuffer();
		while(m.find()) {
			String parameter = m.group(1);
			m.appendReplacement(sb, "\‘\" + " + parameter + " + \"\‘");
		}
		m.appendTail(sb);
		return sb.toString();
	}
}



字符串转换:sql

标签:

原文地址:http://my.oschina.net/u/1583086/blog/505066

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!