Ant Script to Update Mysql Table
This example illustrates how to insert and update data in
table through the build.xml file by simply running the ant command.
In this build.xml file, we are using 4 property elements for connectivity
from database. The first property <property
name="sql.driver"> is used to connect from the sql driver. The
second property <property
name="sql.url"> is used to define the database url and
database name. The third property
<property name="sql.user"> is used to define user
name of the database. The fourth property <property
name="sql.pass"> is used to define the password name of the
database.
In this build.xml file, <target
name="createTables"> is used to execute the query which is in
the client.sql and project.sql file and <target
name"insertData"> is used to execute the query which is in the
insertclient.sql and insertproject.sql file and <target
name="updateTable"> is used to execute the query of updateclient.sql
and updateproject.sql file. The source code of the build.xml file
is as follows:
<project name="MysqlCreateTableAndInsertData" basedir="." default="updateTable"> <property name="sql.driver" value="org.gjt.mm.mysql.Driver"/> <property name="sql.url" value="jdbc:mysql://192.168.10.211/test"/> <property name="sql.user" value="sandeep"/> <property name="sql.pass" value="sandeep"/> <target name="createTables" > <sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" > <transaction src="client.sql"/> <transaction src="project.sql"/> </sql> </target> <target name="insertData" depends="createTables"> <sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" > <transaction src="insertclient.sql"/> <transaction src="insertproject.sql"/> </sql> </target> <target name="updateTable" depends="insertData"> <sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" > <transaction src="updateclient.sql"/> <transaction src="updateproject.sql"/> </sql> </target> </project> |
client.sql
create
table client ( |
project.sql
create
table project ( |
insertclient.sql
INSERT
INTO client (client_name) VALUES ("Galanthus nivalis"); |
insertproject.sql
INSERT
INTO project (project_name) VALUES ("codingdiary.com"); |
updateclient.sql
UPDATE client SET client_name = "Mr. Dormet" WHERE client_id = "13"; |
updateproject.sql
UPDATE project SET project_name = "onedatingtips.com" WHERE project_id = "13"; |
Create the all client.sql, project.sql, insertclient.sql,
insertproject.sql, updateclient.sql, updateproject.sql files parallel of
the build.xml file and simply run the build.xml file with ant
command in the appropriate path on command prompt. If the program executes
successfully, then the following output will be displayed.