Home Tutorials Ant Ant Script to Insert Data in Mysql Table
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Ant Script to Insert Data in Mysql Table

Advertisement
This example illustrates how to insert data in table through the build.xml file by simply running the ant command.

Ant Script to Insert Data in Mysql Table

     

This example illustrates how to insert 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 element <property name="sql.driver"> is used to connect from the sql driver. The second element <property name="sql.url"> is used to define the database url and database name. The third element <property name="sql.user"> is used to define user name of the database. The fourth element <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. The source code of the build.xml file is as follows: 

 

<project name="MysqlCreateTableAndInsertData" basedir="." default="insertData">

  <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>

</project>

client.sql

create table client (
  client_id int not null auto_increment primary key,
  client_name text not null
);

project.sql

create table project (
  project_id int not null auto_increment primary key,
  project_name text not null
);

insertclient.sql

INSERT INTO client (client_name) VALUES ("Galanthus nivalis");
INSERT INTO client (client_name) VALUES ("Narcissus pseudonarcissus");
INSERT INTO client (client_name) VALUES ("Narcissus poeticus var. recurvus");
INSERT INTO client (client_name) VALUES ("Leucojum vernum");
INSERT INTO client (client_name) VALUES ("Iris pseudacorus");
INSERT INTO client (client_name) VALUES ("Crocus tommasinianus");
INSERT INTO client (client_name) VALUES ("Colchicum autumnale");
INSERT INTO client (client_name) VALUES ("Hyacinthoides non-scripta");
INSERT INTO client (client_name) VALUES ("Erythronium dens-canis");
INSERT INTO client (client_name) VALUES ("Fritillaria meleagris");
INSERT INTO client (client_name) VALUES ("Cyclamen coum");
INSERT INTO client (client_name) VALUES ("Tulipa turkestanica");
INSERT INTO client (client_name) VALUES ("Ranunculus ficaria");

insertproject.sql

INSERT INTO project (project_name) VALUES ("codingdiary.com");
INSERT INTO project (project_name) VALUES ("roseindia.net");
INSERT INTO project (project_name) VALUES ("allcooljobs.com");
INSERT INTO project (project_name) VALUES ("huntarticles.com");
INSERT INTO project (project_name) VALUES ("onlinefreetrading.com");
INSERT INTO project (project_name) VALUES ("allcoolloans.com");
INSERT INTO project (project_name) VALUES ("newstrackindia.com");
INSERT INTO project (project_name) VALUES ("artsandlitreture.com");
INSERT INTO project (project_name) VALUES ("javajazzup.com");
INSERT INTO project (project_name) VALUES ("whitecollers.com");
INSERT INTO project (project_name) VALUES ("singlepane.com");
INSERT INTO project (project_name) VALUES ("ilikeyoutube.com");
INSERT INTO project (project_name) VALUES ("fake.com");

Create the all client.sql, project.sql, insertclient.sql, insertproject.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.


Download Source Code

     

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 6, 2008

Ask Questions?    Discuss: Ant Script to Insert Data in Mysql Table  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Moh
March 21, 2012
UmAVDLdzexVjjU

Denny,There IS a bug in the incrementing of the major iersvon. I've fixed it in SVN, but haven't made a new public release with the fix (one more entry on my list of things to do "sometime"). Since the major iersvon only updates between migrations, the bug can only manifest itself if there is an issue with the first step of a given migration, but you're right, there is an issue there. The minor iersvons (steps within a migration) are double protected: the increment happens second, and the whole thing is wrapped in a CFTRANSACTION. The reason the major iersvon increments first is for internal bookkeeping reasons. It's safe for it to increment even if the first migration step fails, because it'll be minor iersvon zero (which means the first step is still next in line). The bug was in when the minor iersvon was reset to zero; it happened at the wrong time before.For the two-developer conflict scenario, there's nothing the tool can do about it. You have to resolve the conflicts when the second developer commits his/her changes. Migration code does have some ordering ramifications, so resolving a conflict might not be as simple as otherwise (quite possibly requiring manual tweaks to the `schema_version` table), but it's really no different from any other conflict between multiple developers.
Sai
May 22, 2012
Y am I No?

M not able to use MySql in this.. Y me No?
DMCA.com