How to use ANT automatic database related operations

zhaozj2021-02-16  52

How to use ANT automatic database related operations

Zhao Caiwen (Vipcowrie@sina.com) Copyright

Today, whether it is CS-based or BS-based architecture, if you deal with the database, then programmers often perform some repetitive operations for the database when developing the system, or when performing unit testing, such as initialization, Building a library, deleting a table, insert test data, etc. Every time you have to test, because the environment is required to be the most basic, the initial environment, the programmer must manually execute some SQL statements, so that our programmer is very low.

Ant, is a Java-based building tool launched by Apache. It is very like a Make tool in the Class C. As long as you write a building program, it is like the script of the drama, it will automatically compile your request, Run, release, etc.

Ant's download address is: http://ant.apache.org

Ant is basically constructed by a Target (task), you can build N Target inside a project, and each Target can independently depending on (Depends) other tasks, that is, only other other When the task is completed, this task is likely to be executed.

There are many built-in Task in Ant, which can be used to build Targets. For example, you have to complete a compiled task, then you can quote this Task (Javac), it looks like a command inside the Java, in fact, Ant to him Packed. E.g:

destdir = "$ {build}"

Classpath = "xyz.jar"

Debug = "on"

/>

This task will compile all the source code to the target folder Build, use the classpath is xyz.jar and is the debug mode.

There is also a very useful task in Ant is a Task (SQL) related to the database operation, and its principle is as follows:

Perform a series of SQL statements via JDBC, the statement can be read from the text file or in the task by being included. The interval between statements is used ";" or the segmentation symbol you define. Note The statement is used by REM or "-". You can use the AutoCommit property to control whether the statement is really submitted, you can also use the onlineRROR attribute to control what should I do when you encounter an error during the execution? (Abort, Continue, STOP).

It has the following properties:

Attribute

Description

Required

Driver

Class Name of the JDBC Driver

YES

URL

Database Connection URL

YES

UserID

Database User Name

YES

Password

Database Password

YES

SRC

File Containing SQL Statements

Yes, UnsSS ENCLOSED WITHIN TAGS

ENCODING

The Encoding of the Files Containing SQL Statements

NO - Defaults to Default JVM EncodingDelimiter

String That Separates SQL Statements

NO, Default ";"

AutoCommit

Auto Commit Flag for Database Connection (Default False)

NO, Default "false"

Print

Print Result Sets from The Statements (Default False)

NO, Default "false"

Showheaders

Print Headers for Result Sets from The Statements (Default True)

NO, Default "True"

OUTPUT

Output File for Result Sets (Defaults to System.out)

NO (Print to System.out by default)

Append

WHETER OUTPUT SHOULD be appended to or overwrite an existing file. Defaults to false.

NO

Classpath

Classpath Used to Load Driver

NO (Use system classpath)

Classpathref

The classpath to use, given as a reference to a path defined elsewhere.

NO (Use system classpath)

Onerror

Action to Perform When Statement Fails: Continue, Stop, Abort

NO, Default "Abort"

RDBMS

Execute Task Only if this rdbms

NO (no restriction)

Version

Execute Task Only if Rdbms Version Match

NO (no restriction)

Caching

SHOULD THE TASK CACHE LOADERS AND THE DRIVER?

NO (default = true)

example:

If we have a task, we must initialize the entire database when you run. The SQL statement to be executed is as follows:

Drop Table CHILD;

DROP TABLE TOY;

Create Table CHild (ID Number, Name Varchar (200));

Create Table Toy (T_ID Number, C_ID Number, Description Varchar (200);

INSERT INTO CHILD VALUES (1, "Tom");

INSERT INTO TOY VALUES (1, 1, "a Baby");

We store the above statement as a file called DDL.sql, and then establish ANT education book as follows:

Driver = "Oracle.jdbc.driver.OracleDriver"

URL = "JDBC: Oracle: Thin: @localhost: Test" Userid = "TEST"

Password = "TEST"

ClasspathRef = "classes12.jar"

SRC = "$ {ddl.sql}"

οNERRοR = "Continue"

/>

In this way, we have established Ant's taught, store it to build.xml, under the command line mode, run the following command:

Ant dbinit

The results are as follows:

Dbinit:

[echo] CREATE DATABASE Schema ... if Error Occurs, Let IT BE

[SQL] Executing File: D: /jbproject/test/db/ddl.sql

[SQL] 6 of 6 SQL Statements Executed SUCCESSFULLY

Build Successful

In this way, we have decreased a lot every time.

转载请注明原文地址:https://www.9cbs.com/read-23206.html

New Post(0)