我們在做程序的時候有事后會涉及到利用sql文件 直接執行,可是在sql文件中有很多注釋,我們要一句一句的執行首先必須的得把sql文件解析
去除其中的注釋,還有把每一句sql語句取出來,然后再利用各個平臺中的數據庫相關執行它。
接下來放代碼!
java版本的
| 001 | packagecom.zz; | 
| 002 | 
| 003 | importjava.io.*; | 
| 004 | importjava.util.ArrayList; | 
| 005 | importjava.util.Enumeration; | 
| 006 | importjava.util.List; | 
| 007 | importjava.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. | 
| 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 | } |