ant使用sample(数据库连接)

本文介绍了一个使用Ant脚本进行Oracle数据库设置的过程,包括创建用户、建立表及删除操作。通过配置文件定义数据库连接属性,并利用Ant任务实现数据库的安装与卸载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是一个sample,还没有完成说明。
properties
oracle.service = sample
oracle.host = localhost
oracle.port = 1521
jdbcdriver.path = ....\ojdbc14.jar



<project name="sql_sample" default="usage" basedir=".">
<property name="prop.file" value="sample.properties" />
<property file="${prop.file}" />
<property name="install.dir" value="${sample.home}/install" />
<property name="conf.dir" value="${sample.home}/conf" />
<property name="sql.dir" value="${sample.home}/sql" />
<property name="sql.table.dir" value="${sql.dir}/table" />
<property name="jdbc.url" value="jdbc:oracle:thin:@${oracle.host}:${oracle.port}:${oracle.service}" />
<property name="jdbc.driver.class" value="oracle.jdbc.driver.OracleDriver" />

<property name="jdbc.sample.user" value="sample" />
<property name="jdbc.sample.password" value="sample" />

<path id="jdbcdriver.class.path">
<pathelement location="${jdbcdriver.path}" />
</path>

<!-- =======================================================
Main Targets.
======================================================= -->
<target name="INSTALL" >
<antcall target="echo_prop" />
<antcall target="assertion" />
<antcall target="sql_create_user" />
<antcall target="sql_create_table" />
</target>

<target name="UNINSTALL">
<antcall target="echo_prop" />
<antcall target="assertion" />
<antcall target="sql_delete_table" />
<antcall target="sql_drop_user" />
</target>

<target name="usage">
<echo message="Usage : ant -f <THIS_FILE> -Doracle.system.password=<ORACLE_SYSTEM_PASSWORD> <TARGET>" />
<echo message="Example : ant -f ant_setup_db.xml -Doracle.system.password=change_on_install INSTALL" />
</target>

<!-- =======================================================
Database Management
======================================================= -->
<target name="sql_connect">
<echo message="jdbcUrl = ${jdbc.url}" />
<echo message="jdbc.driver.class = ${jdbc.driver.class}"/>
<echo message="oracle.system.password = ${oracle.system.password}"/>
<echo message="jdbcdriver.class.path = ${jdbcdriver.class.path}"/>
<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" output="${install.dir}/assertion_readBy_ant.log" append="true" onerror="continue">
SELECT SYSDATE FROM DUAL;
</sql>
</target>

<target name="sql_create_user">
<echo message="jdbcUrl = ${jdbc.url}" />
<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" onerror="continue">
CREATE USER sample PROFILE DEFAULT IDENTIFIED BY sample DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP ACCOUNT UNLOCK;
GRANT CONNECT TO sample;
GRANT RESOURCE TO sample;
GRANT CREATE PUBLIC SYNONYM TO sample;
GRANT DROP PUBLIC SYNONYM TO sample;
GRANT UNLIMITED TABLESPACE TO sample;
ALTER USER sample DEFAULT ROLE ALL;
</sql>
</target>

<target name="sql_drop_user">
<echo message="jdbcUrl = ${jdbc.url}" />
<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" onerror="abort">
DROP USER sample CASCADE;
</sql>
</target>

<target name="sql_create_table">
<echo message="jdbcUrl = ${jdbc.url}" />
<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="sample" url="${jdbc.url}" userid="sample" autocommit="true" print="true" onerror="continue">
<transaction src="${sql.table.dir}/createtables.sql" />
<transaction src="${sql.table.dir}/inittables.sql" />
</sql>
</target>

<target name="sql_delete_table">
<echo message="jdbcUrl = ${jdbc.url}" />
<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="sample" url="${jdbc.url}" userid="sample" autocommit="true" print="true" onerror="abort">
<transaction src="${sql.table.dir}/droptables.sql" />
</sql>
</target>

<!-- =======================================================
Utility
======================================================= -->
<target name="echo_prop" >
<echo message="echo poperties" />
<echo message="oracle.service = ${oracle.service}" />
<echo message="oracle.host = ${oracle.host}" />
<echo message="oracle.port = ${oracle.port}" />
<echo message="jdbcdriver.path = ${jdbcdriver.path}" />
</target>

<!-- write assertion_readBy_InstallScript.log -->
<target name="assertion" >
<delete file="${install.dir}/assertion_readBy_ant.log" failonerror="false" />
<delete file="${install.dir}/assertion_readBy_InstallScript.log" failonerror="false" />
<antcall target="sql_connect" />
<loadfile property="assertionLog" srcfile="${install.dir}/assertion_readBy_ant.log" />
<condition property="assertion">
<and>
<contains string="${assertionLog}" substring="SYSDATE" casesensitive="false" />
</and>
</condition>
<antcall target="write_assertion" />
<fail message="VerifyError : Please Check Oracle Database is running..." unless="assertion" />
</target>

<target name="write_assertion">
<antcall target="write_assertion_OK" />
<antcall target="write_assertion_NG" />
</target>

<target name="write_assertion_OK" if="assertion">
<echo file="${install.dir}/assertion_readBy_InstallScript.log" append="false">OK</echo>
</target>

<target name="write_assertion_NG" unless="assertion">
<echo file="${install.dir}/assertion_readBy_InstallScript.log" append="false">NG</echo>
</target>

</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值