一个.sql数据库脚本如何通过Ant构建到数据库中,例子是网上的,调试过程中遇到不少问题,记录如此:
你所需要的文件:
ant.sql 测试用的文件,可以自己编写
mysql-connector-java-5.1.6-bin.jar 连接数据库驱动jar包
方法一 ant_script.xml
<?xml version="1.0" encoding="UTF-8"?><project basedir="." default="DBrestore" name="DBrestorProject"><target description="Executes an SQL Script" name="DBrestore"> <sql classpath="mysql-connector-java-5.1.6-bin.jar" driver="com.mysql.jdbc.Driver" src="ant.sql" url="jdbc:mysql://localhost:3306/?autoReconnect=true" userid="root" password="root"/></target></project>
方法二 build.properties和build.xml
配置
# Sample ant build properties file#Database URLtest.database.url=jdbc\:mysql\://localhost\:3306/?autoReconnect\=true# Database JDBC Library and Drivertest.jdbc.jar=mysql-connector-java-5.1.6-bin.jartest.jdbc.driver=com.mysql.jdbc.Driver#Database username and password, used for connecting databasetest.database.user=root #Database password test.database.pass=root #Autopopulate sql filetest.sql.src=ant.sql
ant脚本
<?xml version="1.0" ?> <project name="MySQLDB" default="restoredatabase"> <property location="build.properties" name="build.settings" /> <property file="${build.settings}"/> <!-- Loads an SQL file and Updates the database --> <target description="Executes an SQL Script" name="restoredatabase"> <sql classpath="${test.jdbc.jar}" driver="${test.jdbc.driver}" src="${test.sql.src}" url="${test.database.url}" userid="${test.database.user}" password="${test.database.pass}" /> </target> </project>
运行:
1、 >ant -buildfile ant_script.xml
2、 >ant
结果一样。
注意编写脚本时候一定要非常小心对应名称,哪怕多写一个空格都会出现问题。
相关资源:敏捷开发V1.0.pptx