用ant脚本执行sql语句

用ant脚本执行sql语句,第1张

<target name="initialize-database" depends="export-dbschema">

<echo message="Initializing database... If error occurs,let it be." />

<sql

driver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/数据库名"

userid="root"

password="root"

src="${123.sql.dir}/werwerewr.sql"放在src目录下的123目录下的sql目录,werwerewr.sql就是sql的名字

encoding="UTF-8"

onerror="continue">

<classpath refid="classpath"/>

</sql>

</target>

build.xml命名为然后右键run。。。。

有两种方法, 一个是用java写自定义的ant task来解密传给ant script的密码. 然后用明文密码来连接数据库. 写java的缺点在于每次都要事先编译好这个ant task. 还有一种方法是在ant script写一个javascript task. 当然javascript可以调用java的解密函数, 这样不用每次运行ant script前都编译task. 具体脚本如下:

<project name="sfamx.cleanupdb" default="cleanup.db.logging"><dirname property="sfamx.ddl.dir" file="${ant.file.sfamx.ddl.build}"/><target name="cleanupall" depends="replace.db.password, cleanup.db.logging"/><path id="jdbc.classpath"><fileset dir="${db.driver.path}" includes="*.jar"/></path><property name="plaintext.password" value="${plainText}"/><target name="replace.db.password"><script language="javascript"><classpath><!-- for xxx 3.2 --><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.security.tibcrypt_2.15.0.009.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.slf4j.log4j12_1.6.200.001/slf4j-log4j12-1.5.2.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.slf4j.log4j12_1.6.200.001/slf4j-api-1.5.2.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.apache.log4j_1.2.400.002/log4j-1.2.15.jar"/><!-- for xxx 3.1.5 --><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/com.tibco.security.tibcrypt_2.14.300.006.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.slf4j.log4j12_1.5.101.001/slf4j-log4j12-1.5.2.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.slf4j.log4j12_1.5.101.001/slf4j-api-1.5.2.jar"/><pathelement location="${tibco.home}/components/shared/1.0.0/plugins/co.apache.log4j_1.2.300.004/log4j-1.2.15.jar"/></classpath><![CDATA[ importPackage(com.tibco.security)var encryptedPassword = project.getProperty("admin.db.password")var decryptedPassword = ObfuscationEngine.decrypt(encryptedPassword)var plainText = new java.lang.String(decryptedPassword)project.setProperty("plaintext.password", plainText) ]]></script></target><target name="cleanup.db.logging" depends="replace.db.password" description="Run ddl to cleanup commonlogging tables in external database for amx administrator"><sql driver="${admin.db.driver}" url="${admin.db.url}" userid="${admin.db.username}" password="${plaintext.password}" print="yes"><classpath refid="jdbc.classpath"/><transaction src="./${db.ddl.path}/drop_commonlogging.ddl"/></sql></target></project>

输出

05/25/12 18:46:55.406 INFO: [ProcessWrapper] Executing: D:\build\fabric\DSEngine\resources\win32\wrappid.exe,D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\antcall.pid,D:/build/fabric/DSEngine/work/yangxiaoming-lt-1/tibcoamx/tibcojre/1.6.0/bin/java,-cp,D:/build/fabric/DSEngine/work/yangxiaoming-lt-1/tibcoamx/tools/lib/antpackage.jar,org.apache.tools.ant.launch.Launcher,-f,D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\scripts\cleanupDDL\cleanupDB.xml,-Dadmin.db.password=#!Panbl6s1FhkBUM70G9T+okR+fv5Nwoq+fYUx+SLzZi8=,-Dadmin.db.url=jdbc:sqlserver://192.168.71.99:1433databasename=amxdb,-Dadmin.db.username=sa,-Ddb.ddl.path=mssql300,-Ddb.driver.path=D:\build\fabric\DSEngine\work\yangxiaoming-lt-1/fabric/mssql2008_driver,-Dtibco.home=D:/build/fabric/DSEngine/work/yangxiaoming-lt-1/tibcoamx,-Dadmin.db.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver,cleanup.db.logging,

05/25/12 18:46:55.609 INFO: [ProcessWrapper] [STDOUT] Buildfile: D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\scripts\cleanupDDL\cleanupDB.xml

05/25/12 18:46:55.875 INFO: [ProcessWrapper] [STDOUT]

05/25/12 18:46:55.875 INFO: [ProcessWrapper] [STDOUT] replace.db.password:

05/25/12 18:46:55.984 INFO: [ProcessWrapper] [STDOUT][script] log4j:WARN No appenders could be found for logger (com.tibco.security.TIBCOSecurity).

05/25/12 18:46:55.984 INFO: [ProcessWrapper] [STDOUT][script] log4j:WARN Please initialize the log4j system properly.

05/25/12 18:46:56.500 INFO: [ProcessWrapper] [STDOUT] [echo] Tibc02008

05/25/12 18:46:56.500 INFO: [ProcessWrapper] [STDOUT]

05/25/12 18:46:56.500 INFO: [ProcessWrapper] [STDOUT] cleanup.db.logging:

05/25/12 18:46:56.687 INFO: [ProcessWrapper] [STDOUT] [sql] Executing resource: D:\build\fabric\DSEngine\work\yangxiaoming-lt-1\scripts\cleanupDDL\mssql300\drop_commonlogging.ddl

05/25/12 18:46:56.687 INFO: [ProcessWrapper] [STDOUT] [sql] 0 rows affected

05/25/12 18:46:56.687 INFO: [ProcessWrapper] [STDOUT] [sql] 1 of 1 SQL statements executed successfully

05/25/12 18:46:56.703 INFO: [ProcessWrapper] [STDOUT]

05/25/12 18:46:56.703 INFO: [ProcessWrapper] [STDOUT] BUILD SUCCESSFUL

05/25/12 18:46:56.703 INFO: [ProcessWrapper] [STDOUT] Total time: 1 second

在ant中使用javascript, 在javascript又调用了java, 所以一定要注意数据的boxing和unboxing. 比如用java的方法返回decryptedPassword 时, 它其实已经被unboxing了, 失去了java的char[]类型, 但内在传递的值是不变的, 所以需要对它重新boxing, 让它成为java的String类型, 在boxing他的char[]值同时解码, 获得实际字符串, 也就是明文. 再将它传递给javascript的变量.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9394504.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存