How to use ANT automatic database related operations

xiaoxiao2021-03-06  17

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:

AttributeDescriptionRequireddriverClass name of the jdbc driverYesurlDatabase connection urlYesuseridDatabase user nameYespasswordDatabase passwordYessrcFile containing SQL statementsYes, unless statements enclosed within tagsencodingThe encoding of the files containing SQL statementsNo - defaults to default JVM encodingdelimiterString that separates SQL statementsNo, default ";" autocommitAuto commit flag for database connection (default false) No, default "false" printPrint result sets from the statements (default false) No, default "false" showheadersPrint headers for result sets from the statements (default true) No, default "true" outputOutput file for result sets (defaults to System.out) No (print to System.out by default) appendwhether output should be appended to or overwrite an existing file. Defaults to false.NoclasspathClasspath used to load driverNo (use system classpath) classpathrefThe classpath to use, given as a reference to a path defined elsewhere.no (use system classpath) OneRR orAction to perform when statement fails:? continue, stop, abortNo, default "abort" rdbmsExecute task only if this rdbmsNo (no restriction) versionExecute task only if rdbms version matchNo (no restriction) cachingShould 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-44753.html

New Post(0)