public class FieldDatabase extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Inserts the results of a database query into a WordprocessingML table.
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getConnection()
Gets a connection to the data.
|
java.lang.String |
getFileName()
Gets the complete path and file name of the database
|
java.lang.String |
getFirstRecord()
Gets the integral record number of the first data record to insert.
|
java.lang.String |
getFormatAttributes()
Gets which attributes of the format are to be applied to the table.
|
boolean |
getInsertHeadings()
Gets whether to insert the field names from the database as column headings in the resulting table.
|
boolean |
getInsertOnceOnMailMerge()
Gets whether to insert data at the beginning of a merge.
|
java.lang.String |
getLastRecord()
Gets the integral record number of the last data record to insert.
|
java.lang.String |
getQuery()
Gets a set of SQL instructions that query the database.
|
int |
getSwitchType(java.lang.String switchName) |
java.lang.String |
getTableFormat()
Gets the format that is to be applied to the result of the database query.
|
void |
setConnection(java.lang.String value)
Sets a connection to the data.
|
void |
setFileName(java.lang.String value)
Sets the complete path and file name of the database
|
void |
setFirstRecord(java.lang.String value)
Sets the integral record number of the first data record to insert.
|
void |
setFormatAttributes(java.lang.String value)
Sets which attributes of the format are to be applied to the table.
|
void |
setInsertHeadings(boolean value)
Sets whether to insert the field names from the database as column headings in the resulting table.
|
void |
setInsertOnceOnMailMerge(boolean value)
Sets whether to insert data at the beginning of a merge.
|
void |
setLastRecord(java.lang.String value)
Sets the integral record number of the last data record to insert.
|
void |
setQuery(java.lang.String value)
Sets a set of SQL instructions that query the database.
|
void |
setTableFormat(java.lang.String value)
Sets the format that is to be applied to the result of the database query.
|
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, updatepublic java.lang.String getFormatAttributes()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setFormatAttributes(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - Which attributes of the format are to be applied to the table.java.lang.Exceptionpublic java.lang.String getConnection()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setConnection(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - A connection to the data.java.lang.Exceptionpublic java.lang.String getFileName()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setFileName(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - The complete path and file name of the databasejava.lang.Exceptionpublic java.lang.String getFirstRecord()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setFirstRecord(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - The integral record number of the first data record to insert.java.lang.Exceptionpublic boolean getInsertHeadings()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setInsertHeadings(boolean value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - Whether to insert the field names from the database as column headings in the resulting table.java.lang.Exceptionpublic java.lang.String getTableFormat()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setTableFormat(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - The format that is to be applied to the result of the database query.java.lang.Exceptionpublic boolean getInsertOnceOnMailMerge()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setInsertOnceOnMailMerge(boolean value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - Whether to insert data at the beginning of a merge.java.lang.Exceptionpublic java.lang.String getQuery()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setQuery(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - A set of SQL instructions that query the database.java.lang.Exceptionpublic java.lang.String getLastRecord()
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
public void setLastRecord(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to extract data from a database and insert it as a field into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// This DATABASE field will run a query on a database, and display the result in a table.
FieldDatabase field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getDatabaseDir() + "Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT * FROM [Products]");
Assert.assertEquals(MessageFormat.format(" DATABASE \\d {0} \\c \"DSN=MS Access Databases\" \\s \"SELECT * FROM [Products]\"", getDatabaseDir().replace("\\", "\\\\") + "Northwind.accdb"),
field.getFieldCode());
// Insert another DATABASE field with a more complex query that sorts all products in descending order by gross sales.
field = (FieldDatabase) builder.insertField(FieldType.FIELD_DATABASE, true);
field.setFileName(getMyDir() + "Database\\Northwind.accdb");
field.setConnection("DSN=MS Access Databases");
field.setQuery("SELECT [Products].ProductName, FORMAT(SUM([Order Details].UnitPrice * (1 - [Order Details].Discount) * [Order Details].Quantity), 'Currency') AS GrossSales " +
"FROM([Products] " +
"LEFT JOIN[Order Details] ON[Products].[ProductID] = [Order Details].[ProductID]) " +
"GROUP BY[Products].ProductName " +
"ORDER BY SUM([Order Details].UnitPrice* (1 - [Order Details].Discount) * [Order Details].Quantity) DESC");
// These properties have the same function as LIMIT and TOP clauses.
// Configure them to display only rows 1 to 10 of the query result in the field's table.
field.setFirstRecord("1");
field.setLastRecord("10");
// This property is the index of the format we want to use for our table. The list of table formats is in the "Table AutoFormat..." menu
// that shows up when we create a DATABASE field in Microsoft Word. Index #10 corresponds to the "Colorful 3" format.
field.setTableFormat("10");
// The FormatAttribute property is a string representation of an integer which stores multiple flags.
// We can patrially apply the format which the TableFormat property points to by setting different flags in this property.
// The number we use is the sum of a combination of values corresponding to different aspects of the table style.
// 63 represents 1 (borders) + 2 (shading) + 4 (font) + 8 (color) + 16 (autofit) + 32 (heading rows).
field.setFormatAttributes("63");
field.setInsertHeadings(true);
field.setInsertOnceOnMailMerge(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.DATABASE.docx");
value - The integral record number of the last data record to insert.java.lang.Exceptionpublic int getSwitchType(java.lang.String switchName)