ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Loughran <ste...@apache.org>
Subject Re: SQLExec Task
Date Wed, 03 Oct 2007 12:22:53 GMT
Subir Bhaumik wrote:
> Hi,
>  I'm using ant 1.6.5. I'm trying to run sql files in a row using SQLExec task.
> The problem i am facing is that when execute  the task the sql files run and database
and tables are created but at the same time in the console getting the exception like below:
> 
> sql:
> [run_script] connecting to jdbc:mysql://localhost/sqltask
> [run_script] Loading org.gjt.mm.mysql.Driver using a cached AntClassLoader.
> [run_script] Opening PrintStream to output file C:\bishw\temp\log.txt
> [run_script] Executing file: C:\bishw\temp\bishwakarma.sql
> [run_script] SQL:  CREATE DATABASE bishwakarma123
> [run_script] Failed to execute:  CREATE DATABASE bishwakarma123
> [run_script] java.sql.SQLException: General error: Can't create database 'bishwakarma123'.
Database
> exists
> 
> But there was no database named 'bishwakarma123' befor run.
> 
> 
> My sql file looks like :
> 
> CREATE DATABASE bishwakarma123;
> 
> USE bishwakarma123;
> 
> --
> -- Table structure for table `admin_user`
> --
> 
> CREATE TABLE admin_user (
>   user_id varchar(25),
>   password varchar(25) ,
>   PRIMARY KEY  (user_id)
> ) TYPE=InnoDB;
> 
> 
> 
> Can any one say me why i am getting such error and how it can be solved.
> 
> And another thing i want to know
> 
> How can i get the exact sequence of execution of sql files in the fileset?

Use a filelist instead, or inline scripts.

If you dont have to do it inside your own code, try getting it working 
in a build file; it can hide from problems.

> Am i bound to give an existing database in the connection url? Otherwise it gives UNKNOWN
Database error while connecting. It's o.k. if there is no such database then how can it connect
to the database. Is there any way to use the same database in the connection url as is in
the sql file which i want to run and here it is 'bishwakarma123'.

Here is the build.xml file from Chapter 16 of Ant in Action, where I 
cover database setup, among other things.

I first create a mysql admin task with the URL set to 
"jdbc:mysql://localhost/mysql", user=root, password=the root mysql password

Then I create a new database (dropping the existing one if I need to), 
and go on to issue commands on there. I order things just by having 
multiple sql commands.

-steve

<project name="mysql" default="deploy"
   xmlns:ivy="antlib:fr.jayasoft.ivy.ant"
   xmlns:ext="http://antbook.org/d1/ext" >

   <description>
     Add database setup the web application.
   </description>

   <property file="build.properties"/>
   <import file="webapp-chapter-14.xml"/>


   <!-- ===============================  -->
   <!-- Now comes the mySQL stuff of chapter 14 -->
   <!-- ===============================  -->
   <!-- Initialise MySQL -->

   <target name="mysql-init" depends="ivy-resolve"
     xmlns:ext="http://antbook.org/d1/ext" >
     <ivy:cachepath pathid="sql.classpath" conf="sql" />
     <property name="mysql.root.pass" value=""/>
     <property name="mysql.diary.pass" value="secret"/>
     <presetdef name="mysql-admin"
       uri="http://antbook.org/d1/ext">
       <sql driver="com.mysql.jdbc.Driver"
         classpathref="sql.classpath"
         userid="root"
         password="${mysql.root.pass}"
         print="true"
         url="jdbc:mysql://localhost/mysql"
         expandProperties="true"/>
     </presetdef>
     <presetdef name="mysql"
       uri="http://antbook.org/d1/ext">
       <ext:mysql-admin
         userid="diary"
         password="${mysql.diary.pass}"
         url="jdbc:mysql://localhost/diary"/>
     </presetdef>
   </target>

   <target name="mysql-drop-db" depends="mysql-init"
     description="create the database and account">
     <ext:mysql-admin onerror="continue">
       DROP DATABASE diary;
     </ext:mysql-admin>
   </target>


   <target name="mysql-create-db"
     depends="mysql-init,mysql-drop-db"
     description="create the database and account">
     <ext:mysql-admin>
       CREATE DATABASE diary;
       GRANT ALL PRIVILEGES ON diary.* TO 'diary'@'localhost';
       SET PASSWORD FOR 'diary'@'localhost' =
        PASSWORD('${mysql.diary.pass}');
     </ext:mysql-admin>
   </target>

<target name="mysql-create-events" depends="mysql-init"
     description="create the event table">
   <ext:mysql>
     CREATE TABLE EVENTS (
     EVENT_ID varchar(64) not null,
     EVENT_NAME varchar(255) not null,
     EVENT_DATE timestamp not null,
     EVENT_VERSION integer,
     EVENT_LASTMODIFIED timestamp,
     EVENT_TEXT varchar(8192),
     primary key (EVENT_ID));
   </ext:mysql>
</target>

<target name="mysql-create-events-file" depends="mysql-init">
   <ext:mysql>
     <transaction src="config/deploy/sql/create_event_table.sql" />
   </ext:mysql>
</target>

<target name="mysql-list-events" depends="mysql-init"
   description="list all events in the system">
   <ext:mysql>
     select * from EVENTS;
   </ext:mysql>
</target>

<target name="mysql-drop-events" depends="mysql-init"
   description="delete the event table">
   <ext:mysql>
     DROP TABLE EVENTS;
   </ext:mysql>
</target>

<target name="mysql-setup"
   depends="mysql-create-db,mysql-create-events"/>

   <target name="init-datasource" depends="mysql-init,deploy-properties">
     <property name="mysql-ds.xml" location="xml/diary-ds.xml" />
     <property name="deployed.datasource.xml"
       location="${deploy.dir}/diary-ds.xml"/>
   </target>


   <target name="ready-to-deploy"
 
depends="webapp-chapter-14.ready-to-deploy,init-datasource,deploy-properties" 
/>

   <target name="copy-datasource" depends="ready-to-deploy">
     <copy file="${mysql-ds.xml}" tofile="${deployed.datasource.xml}" >
       <filterchain>
         <expandproperties/>
       </filterchain>
     </copy>
   </target>

   <!--override point-->
   <target name="deploy" depends="deploy-by-copy,copy-datasource"/>

   <!--override point-->
   <target name="undeploy" depends="undeploy-by-copy,init-datasource"
     description="undeploy">
     <delete file="${deployed.datasource.xml}" />
   </target>

</project>



-- 
Steve Loughran                  http://www.1060.org/blogxter/publish/5
Author: Ant in Action           http://antbook.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message