<target name="dbmanager" description="Start HSQLDB Manager">
<java classname="org.hsqldb.util.DatabaseManagerSwing"
fork="yes"
classpathref="build.classpath"
failonerror="true">
<arg value="-url"/>
<arg value="jdbc:hsqldb:hsql://localhost/"/>
<arg value="-driver"/>
<arg value="org.hsqldb.jdbcDriver"/>
</java>
</target>
<target name="dbstartup" description="Start the Hsqldb database server">
<java classname="org.hsqldb.Server" fork="yes" classpathref="build.classpath" failonerror="true"/>
</target>
<taskdef name="shutdownTask" classname="ShutdownTask" classpathref="build.classpath"/>
<target name="dbshutdown" description="Stop the Hsqldb database server">
<shutdownTask driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:hsql://localhost/" userid="sa" password=""
sqlCommand="SHUTDOWN COMPACT" msg="MyShutdownTask"/>
</target>
其中shutdownTask依赖于自定义的Task类,代码如下:
//ShutdownTask.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
*ShutdownTask:AntTaskforshutdowntheHsqldbserver.
*
*@authorYuLimin
*/
publicclass ShutdownTask extends Task
{
// Defaut Value
private String msg = "ShutdownTask";
private String driver = "org.hsqldb.jdbcDriver";
private String url = "jdbc:hsqldb:hsql://localhost/";
private String userid = "sa";
private String password = "";
private String sqlCommand = "SHUTDOWN";
public ShutdownTask()
{
super();
}
/**
*ForTest
*
*@paramargv
*@throwsException
*/
publicstaticvoid main(final String[] argv)
{
final ShutdownTask shutdownTask = new ShutdownTask();
shutdownTask.shutdown();
}
/**
*Themethodexecutingthetask
*/
publicvoid execute() throws BuildException
{
System.out.println(msg + " Begin");
System.out.println("Driver=" + getDriver());
System.out.println("URL=" + getUrl());
System.out.println("Userid=" + getUserid());
System.out.println("Password=" + getPassword());
System.out.println("SqlCommand=" + getSqlCommand());
shutdown();
System.out.println(msg + " End");
System.out.println();
}
/**
*Shutdownthedatabase
*/
publicvoid shutdown()
{
Connection connection = null;
Statement statement = null;
try
{
Class.forName(getDriver());
connection = DriverManager.getConnection(getUrl(),getUserid(),getPassword());
statement = connection.createStatement();
statement.execute(getSqlCommand());
statement.close();
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
thrownew RuntimeException(e);
}
finally
{
if(statement != null)
{
try
{
statement.close();
}
catch(SQLException e)
{
thrownew RuntimeException(e);
}
}
if(connection != null)
{
try
{
connection.close();
}
catch(SQLException e)
{
thrownew RuntimeException(e);
}
}
}
}
// More accessor method : setter & getter
publicvoid setMsg(final String msg)
{
this.msg = msg;
}
publicvoid setDriver(final String driver)
{
this.driver = driver;
}
publicvoid setPassword(final String password)
{
this.password = password;
}
publicvoid setUrl(final String url)
{
this.url = url;
}
publicvoid setUserid(final String userid)
{
this.userid = userid;
}
publicvoid setSqlCommand(final String sqlCommand)
{
this.sqlCommand = sqlCommand;
}
public String getDriver()
{
returndriver;
}
public String getPassword()
{
returnpassword;
}
public String getUrl()
{
returnurl;
}
public String getUserid()
{
returnuserid;
}
public String getSqlCommand()
{
returnsqlCommand;
}
public String getMsg()
{
returnmsg;
}
}
调用说明:
<!-- Classpath declaration -->
<pathid="hsqldb.classpath">
<filesetdir="${lib.dir}">
<includename="**/hsqldb.jar"/>
<includename="**/ShutdownTask.jar"/>
</fileset>
</path>
<!-- Shutdown the Hsqldb database server via ShutdownTask -->
<targetname="dbshutdownTask"description="Shutdown the Hsqldb database server via ShutdownTask">
<!-- Define ShutdownTask -->
<taskdefname="shutdownTask"classname="ShutdownTask"classpathref="hsqldb.classpath"/>
<!-- Call ShutdownTask -->
<!-- Default ShutdownTask, only like this -->
<!-- <shutdownTask/> -->
<!-- Sample MyShutdownTask -->
<!-- <shutdownTask driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:hsql://localhost/"
userid="sa" password="" sqlCommand="SHUTDOWN" msg="MyShutdownTask"/> -->
<!-- Sample MyShutdownTask SHUTDOWN COMPACT -->
<!-- <shutdownTask driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:hsql://localhost/"
userid="sa" password="" sqlCommand="SHUTDOWN COMPACT" msg="MyShutdownTask"/> -->
<!-- Sample MyShutdownTask SHUTDOWN IMMEDIATELY -->
<shutdownTaskdriver="org.hsqldb.jdbcDriver"url="jdbc:hsqldb:hsql://localhost/"userid="sa"
password=""sqlCommand="SHUTDOWN IMMEDIATELY"msg="MyShutdownTask"/>
</target>
如果是命令行上启动了hsqldb,直接Ctl+C就可以结束掉它,几秒钟后就生效。