Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Java Ö´ÐÐ SQL ½Å±¾Îļþ

ÊÇ¿½±´µÄ±ðÈ˵ģ¬ÒÔ±¸Ñ§Ï°
package com.unmi.db;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* ¶ÁÈ¡ SQL ½Å±¾²¢Ö´ÐÐ
* @author Unmi
*/
public class SqlFileExecutor {
/**
* ¶ÁÈ¡ SQL Îļþ£¬»ñÈ¡ SQL Óï¾ä
* @param sqlFile SQL ½Å±¾Îļþ
* @return List<sql> ·µ»ØËùÓÐ SQL Óï¾äµÄ List
* @throws Exception
*/
private List<String> loadSql(String sqlFile) throws Exception {
List<String> sqlList = new ArrayList<String>();
try {
InputStream sqlFileIn = new FileInputStream(sqlFile);
StringBuffer sqlSb = new StringBuffer();
byte[] buff = new byte[1024];
int byteRead = 0;
while ((byteRead = sqlFileIn.read(buff)) != -1) {
sqlSb.append(new String(buff, 0, byteRead));
}
// Windows Ï»»ÐÐÊÇ \r\n, Linux ÏÂÊÇ \n
String[] sqlArr = sqlSb.toString().split("(;\\s*\\r\\n)|(;\\s*\\n)");
for (int i = 0; i < sqlArr.length; i++) {
String sql = sqlArr[i].replaceAll("--.*", "").trim();
if (!sql.equals("")) {
sqlList.add(sql);
}
}
return sqlList;
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
/**
* ´«ÈëÁ¬½ÓÀ´Ö´ÐÐ SQL ½Å±¾Îļþ£¬ÕâÑù¿ÉÓëÆäÍâµÄÊý¾Ý¿â²Ù×÷ͬ´¦Ò»¸öÊÂÎïÖÐ
* @param conn ´«ÈëÊý¾Ý¿âÁ¬½Ó
* @param sqlFile SQL ½Å±¾Îļþ
* @throws Exception
*/
public void execute(Connection conn, String sqlFile) throws Exception {
Statement stmt = null;
List<String> sqlList = loadSql(sqlFile);
stmt = conn.createStatement();
for (String sql : sqlList) {
stmt.addBatch(sql);
}
int[] rows = stmt.executeBatch();
System.out.println("Row count:" + Arrays.toString(rows));
}
/**
* ×Ô½¨Á¬½Ó£¬¶ÀÁ¢ÊÂÎïÖÐÖ´ÐÐ SQL Îļþ
* @param sqlFile SQL ½Å±¾Îļþ
* @throws Exception
*/
public void execute(String sqlFile) throws Exception {
Connection conn = DBCente


Ïà¹ØÎĵµ£º

ÐÞ¸Äsql server µÄÁ¬½ÓÊý

 ÈçºÎÐÞ¸ÄSQL ServerµÄÁ¬½ÓÊý
ÎÒ°ÑSQL   Server   7.0µÄÓû§Á¬½ÓÊýÉèΪ1ºó£¬Êý¾Ý¿â¾ÍÔÙÒ²Á¬²»ÉÏÁË£¬ËùÒÔҲû°ì·¨ÐÞ¸ÄÁ¬½ÓÊý  
  ÇëÎÊÓÐʲô°ì·¨ÄÜÐÞ¸ÄÁ¬½ÓÊý£¿
ÔÚserver   µÄÊôÐÔÀïÃæÓиöconnetctions     µÄ  
   
  maximun     concurr ......

¾¡Á¿±ÜÃâÔÚSQLÓï¾äµÄWHERE×Ó¾äÖÐʹÓú¯Êý

----start
    ÔÚSQLÓï¾äµÄ WHERE ×Ó¾äÖÐÓ¦¸Ã¾¡Á¿±ÜÃâÔÚ×Ö¶ÎÉÏʹÓú¯Êý£¬ÒòΪÕâÑù×ö»áʹ¸Ã×Ö¶ÎÉϵÄË÷ÒýʧЧ£¬Ó°ÏìSQLÓï¾äµÄÐÔÄÜ¡£¼´Ê¹¸Ã×Ö¶ÎÉÏûÓÐË÷Òý£¬Ò²Ó¦¸Ã±ÜÃâÔÚ×Ö¶ÎÉÏʹÓú¯Êý¡£¿¼ÂÇÏÂÃæµÄÇé¿ö£º
CREATE TABLE USER
(
NAME VARCHAR(20) NOT NULL,---ÐÕÃû
REGISTERDATE TIMESTAMP---×¢² ......

£¨×îгɹ¦£©Ãâ°²×°OracleÔËÐÐpl/sql developer

1.µ½http://www.oracle.com/technology/global/cn/software/tech/oci/instantclient/htdocs/winsoft.htmlÏÂÔØ
11.1.0.7.0 °æµÄ¼´Ê±¿Í»§¶Ë³ÌÐò°ü — Basic£¨²»ÊÇBasic Lite£©
2.½«ÏÂÔص½µÄÎļþ½âѹ£¬½âѹºóÎÒ½«Ä¿Â¼instantclient_11_1ÀïµÄÈ«²¿Îļþ¿½±´µ½ÁËÒ»¸öеÄĿ¼£ºE:\programs\OracleClient¡£ÄãÒ²¿ÉÒÔ²»¿½±´£¬Ö ......

ÈçºÎÓÃÃüÁîÉú³ÉSQL SERVER½Å±¾

ÈçºÎÓÃÃüÁîÉú³ÉSQL   SERVER½Å±¾  
   
  --ÒÔÏÂÉú³ÉÕû¸öÊý¾Ý¿âµÄSQL½Å±¾£¬ÎÒ²âÊÔÁË£¬Ï൱ºÃÓᣠ 
  --(scptxfr.exeµÄ·¾¶ÒªÕýÈ·ÒÔÏÂÊÇÎҵķ¾¶)  
  declare   @cMd   varchar(1000)  
  set   @cmd   =   'master.dbo.xp_cm ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ