我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析
去除其中的注释,还有把每一句sql语句取出来,然后再利用各个平台中的数据库相关执行它。
接下来放代码!
java版本的
001 | package com.zz; |
002 |
003 | import java.io.*; |
004 | import java.util.ArrayList; |
005 | import java.util.Enumeration; |
006 | import java.util.List; |
007 | import java.util.Vector; |
008 |
009 | /* |
010 |
* 作者 祝君 |
011 |
* 时间 2014年1月16号 |
012 |
* java执行数据库脚本代码 |
013 |
*/ |
014 | public class SqlHelper { |
015 |
016 |
/** |
017 |
* @param args |
018 |
*/ |
019 |
public static void main(String[] args) { |
020 |
|
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]); |
026 |
027 |
} |
028 |
public static String GetText(String path){ |
029 |
File file=new File(path); |
030 |
if(!file.exists()||file.isDirectory()) |
031 |
return null; |
032 |
StringBuffer sb=new StringBuffer(); |
033 |
try |
034 |
{ |
035 |
FileInputStream fis = new FileInputStream(path); |
036 |
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); |
037 |
BufferedReader br = new BufferedReader(isr); |
038 |
String temp=null; |
039 |
temp=br.readLine(); |
040 |
while(temp!=null){ |
041 |
sb.append(temp+"/r/n"); |
042 |
temp=br.readLine(); |
043 |
} |
044 |
} catch (Exception e) { |
045 |
e.printStackTrace(); |
046 |
} |