原文:C#和Java中执行SQL文件脚本的代码(非常有用)
我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析
去除其中的注释,还有把每一句sql语句取出来,然后再利用各个平台中的数据库相关执行它。
接下来放代码!
java版本的
004 |
import java.util.ArrayList; |
005 |
import java.util.Enumeration; |
006 |
import java.util.List; |
007 |
import java.util.Vector; |
014 |
public class SqlHelper { |
019 |
public static void main(String[] args) { |
021 |
String path=new String("d:\\zzadmin.sql"); |
022 |
String sql=GetText(path); |
023 |
String[] arr=getsql(sql); |
024 |
for(int i=0;i<arr.length;i++) |
025 |
System.out.println("第"+i+"句:"+arr[i]); |
028 |
public static String GetText(String path){ |
029 |
File file=new File(path); |
030 |
if(!file.exists()||file.isDirectory()) |
032 |
StringBuffer sb=new StringBuffer(); |
035 |
FileInputStream fis = new FileInputStream(path); |
036 |
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); |
037 |
BufferedReader br = new BufferedReader(isr); |
041 |
sb.append(temp+"\r\n"); |
044 |
} catch (Exception e) { |
047 |
return sb.toString(); |
055 |
public static String[] getsql(String sql) |
058 |
s=s.replace("\r\n","\r"); |
059 |
s=s.replace("\r", "\n"); |
060 |
String[] ret=new String[1000]; |
061 |
String[] sqlarray=s.split(";\n"); |
062 |
sqlarray=filter(sqlarray); |
064 |
for (String item : sqlarray) |
066 |
String ret_item = ""; |
067 |
String[] querys = item.trim().split("\n"); |
068 |
querys = filter(querys);//去空 |
069 |
for (String query : querys) |
071 |
String str1 = query.substring(0, 1); |
072 |
String str2 = query.substring(0, 2); |
073 |
if (str1.equals("#") || str2.equals("--") || str2.equals("/*") || str2.equals("//"))//去除注释的关键步奏 |
087 |
/// <param name="ss">数组</param> |
088 |
/// <returns></returns> |
089 |
public static String[] filter(String[] ss) |
091 |
List<String> strs = new ArrayList<String>(); |
092 |
for (String s : ss) { |
093 |
if (s != null && !s.equals("")) |
097 |
String[] result=new String[strs.size()]; |
098 |
for(int i=0;i<strs.size();i++) |
100 |
result[i]=strs.get(i).toString(); |
106 |
public void deletezs(String fileStr) |
109 |
Vector<String> vec=new Vector<String>(); |
110 |
String str="",tm="",mm=""; |
111 |
BufferedReader br = new BufferedReader( new FileReader(fileStr)); |
113 |
while( null != (str = br.readLine() ) ) |
115 |
if ((str.indexOf("/*")>=0)&&((bol==false))) |
117 |
if (str.indexOf("*/")>0) |
120 |
vec.addElement(str.substring(0,str.indexOf("/*"))+str.substring(str.indexOf("*/")+2,str.length())); |
125 |
mm=str.substring(0,str.indexOf("/*")); |
126 |
if (!(mm.trim().equals(""))) |
132 |
if (str.indexOf("*/")>=0) |
135 |
mm=str.substring(str.indexOf("*/ ")+ 2 ,str.length()); |
136 |
if (!mm.trim().equals( "" )) |
140 |
else if (str.indexOf( "//" )>= 0 ) |
142 |
tm=str.substring( 0 ,str.indexOf( "//" )); |
143 |
if (!tm.trim().equals( "" )) |
152 |
File fName= new File(fileStr); |
153 |
FileWriter in= new FileWriter(fName); |
155 |
Enumeration<String> ew=vec.elements(); |
157 |
while (ew.hasMoreElements()) { |
158 |
ssss= ew.nextElement().toString(); |
165 |
} catch (Exception ee){ |
166 |
ee.printStackTrace(); |
调用GetText就可以返回一个装满了sql语句的数组,循环执行其中的sql语句吧
c#版本的
001 |
//-------------------------第一种------------------------------------- |
003 |
/// 获取sql文件中的sql语句数组 第一种方法 |
005 |
/// <param name="sql"></param> |
006 |
/// <returns></returns> |
007 |
public static string [] sql_split( string sql) |
010 |
Regex reg = new Regex( "/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/" ); |
011 |
reg.Replace(sql, "ENGINE=\\1 DEFAULT CHARSET=utf8" ); |
012 |
s = s.Replace( '\r' , '\n' ); |
013 |
string [] ret = new string [10000]; |
014 |
string [] sqlarray = StringSplit(s, ";\n" ); |
016 |
foreach ( string item in sqlarray) |
019 |
string [] queries = item.Split( '\n' ); |
020 |
queries = filter(queries); |
021 |
foreach ( string query in queries) |
023 |
string str1 = query.Substring(0, 1); |
024 |
string str2 = query.Substring(0, 2); |
025 |
if (str1 != "#" && str2 != "--" && str2 != "/*" && str2 != "//" )//去除注释的关键步奏 |
039 |
/// <param name="ss"></param> |
040 |
/// <returns></returns> |
041 |
public static string [] filter( string [] ss) |
043 |
List< string > strs = new List< string >(); |
044 |
foreach ( string s in ss) |
046 |
if (! string .IsNullOrEmpty(s)) strs.Add(s); |
048 |
string [] result = strs.ToArray(); |
054 |
/// <param name="strSource"></param> |
055 |
/// <param name="strSplit"></param> |
056 |
/// <returns></returns> |
057 |
public static string [] StringSplit( string strSource, string strSplit) |
059 |
string [] strtmp = new string [1]; |
060 |
int index = strSource.IndexOf(strSplit, 0); |
063 |
strtmp[0] = strSource; |
068 |
strtmp[0] = strSource.Substring(0, index); |
069 |
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp); |
076 |
/// <param name="strSource"></param> |
077 |
/// <param name="strSplit"></param> |
078 |
/// <param name="attachArray"></param> |
079 |
/// <returns></returns> |
080 |
private static string [] StringSplit( string strSource, string strSplit, string [] attachArray) |
082 |
string [] strtmp = new string [attachArray.Length + 1]; |
083 |
attachArray.CopyTo(strtmp, 0); |
085 |
int index = strSource.IndexOf(strSplit, 0); |
088 |
strtmp[attachArray.Length] = strSource; |
093 |
strtmp[attachArray.Length] = strSource.Substring(0, index); |
094 |
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp); |
098 |
//----------------------------------------------------- |
100 |
//-----------------------第二种------------------------------ |
102 |
/// 获取sql文件中的sql语句数组 第二种 |
104 |
/// <param name="sql"></param> |
105 |
/// <returns></returns> |
106 |
public string [] getsqls( string sql) |
109 |
s = s.Replace( "\r\n" , "\n" ); |
110 |
s = s.Replace( "\r" , "\n" ).Trim(); |
111 |
string [] ret = new string [1000]; |
113 |
string [] sqlarray= StringSplit(s, ";\n" ); |
114 |
sqlarray = filter(sqlarray); //去空 |
117 |
foreach ( string item in sqlarray) |
119 |
string ret_item = "" ; |
120 |
string [] querys = item.Trim().Split( '\n' ); |
121 |
querys = filter(querys); //去空 |
123 |
foreach ( string query in querys) |
125 |
string str1 = query.Substring(0, 1); |
126 |
string str2 = query.Substring(0, 2); |
127 |
if (str1 == "#" || str2 == "--" || str2 == "/*" || str2 == "//" )//去除注释的关键步奏 |
c#两个方法对sql文件解析都是一样的
- 使用java以及jdbc不使用第三方库执行sql文件脚本
使用java以及jdbc不使用第三方库执行sql文件脚本 2017年02月15日 15:51:45 阅读数:660 使用java执行sql脚本的方法 解析sql脚本,删除不必要的注释和空行 将语句按分 ...
- qt中执行 sql文件的方法
由于qt中没有原生的执行sql文件的方法.因此我们需要根据sql文件中的流的特点,将其分解成一个个语句单独执行. 1.首先通过Qfile读取sql文件 2.将sql文件中的内容通过“:”进行拆解 3. ...
- PHP执行.SQL文件的实例代码分享
介绍下使用PHP执行.SQL文件的代码一例,分享下. demo.php: <?php ) )) ) ENGINE) unsigned ) unsigned )) ) ENGINE) unsign ...
- 去掉utf-8的Bom头:使用java以及jdbc不使用第三方库执行sql文件脚本
package com.xxx.xxx.dao; import java.io.BufferedReader; import java.io.File; import java.io.FileInpu ...
- 运行执行sql文件脚本的例子
sqlcmd -s -d db_test -r -i G:\test.sql 黑色字体为关键命令,其他颜色(从左至右):服务器名称,用户名,密码,数据库,文件路径 通过select @@servern ...
- 在 PL/SQL Developer 中执行SQL文件的方法
打开 command Window SQL> @'D:\My Documents\Downloads\bde_chk_cbo.sql'; 整个路径及文件两边要有单引号哦!
- Java中执行.exe文件
public static void main(String args[]){ try { String command ="notepad"; // 笔记本 Process ch ...
- 在eclipse中执行sql的编码问题
症状-分析: 刚才在eclipse中执行sql文件,发现数据进入数据库的时候总是乱码 后来查看MySQL的编码设置,全是UTF8,没问题,sql文件本身也是UTF8的编码 并且,使用MySQL的CMD ...
- 如何在linux中运行sql文件
1.在linux中进入sql命令行 mysql -u root -p 输入密码 2.假设home下面有a.sql文件 先得use databasename,要不会报错 “No Database S ...
随机推荐
- VMware vSphere 服务器虚拟化之十七 桌面虚拟化之安装View链接服务器
VMware vSphere 服务器虚拟化之十七 桌面虚拟化之安装View链接服务器 View链接服务器(View Connection Server)是Vmware Horizon View桌面虚拟 ...
- SpringSecutiry权限管理手册
SpringSecutiry权限管理手册: 请见以下URL: http://www.mossle.com/docs/auth/html/preface.html Spring Security 参考文 ...
- go - 内置基础类型
Go 语言中包括以下内置基础类型: 布尔型:bool 整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint 浮点型:f ...
- JS判断用户连续输入
方案1 // // $('#element').donetyping(callback[, timeout=1000]) // Fires callback when a user has finis ...
- Oracle Instanc Client安装命令工具
条件 1.Linux RHEL 6.X X86_64操作系统 2.从安装Oracleserver的server此次收购Oracle相关文件(同OS) 软件下载 从Oracle包: 1) instan ...
- 当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
如图所示, 码,如以下:<input id="user_sn" class="loginInput" name="user_sn" t ...
- tolower (Function)
this is a function that Convert uppercase letter to lowercase Converts c to its lowercase equivalent ...
- [勘探开发]成绩,全栈开发,健全&借贷
开发探索的一些update: 将结果做为开发的基础和终极目标 开发人员从过程的追求到最后结果的追求是一个质变的过程.相当于NBA中得分王和总冠军的差别: 一个是完毕一个局部的本职工作(有时候会和项目的 ...
- mod_python模块安装
两.mod_python 1.性能 使用mod_python的主要优势在于比传统CGI更高的性能. 一个測试,使用在Pentium 1.2GHz的机器上执行Red Hat Linux 7.3.使用4种 ...
- 理解JavaScript的闭包
在JS这块,免不了被问什么是闭包. 从一个常见的循环问题说起. 有一个ul列表, 里面有5个li标签,我希望点击每个li标签的时候,弹出每个li标签对应的索引值(第一个弹出0,第二个弹出1...). ...