GroupDocs.Parser provides the functionality to extract data from databases via JDBC. This feature allows you to parse database tables and extract their content as text, similar to how you would extract data from document files.
API Overview
Constructors
The Parser class provides constructors specifically for database extraction:
// Constructors with java.sql.Connection object
Parser(java.sql.Connectionconnection);Parser(java.sql.Connectionconnection,ParserSettingsparserSettings);// Constructor with JDBC connection string
Parser(StringfilePath,LoadOptionsloadOptions);
Optional settings for logging and other parser configurations
Return Types
getToc returns Iterable<TocItem> where each TocItem represents a database table: getText returns the table name and getPageIndex returns the table index
isText and isToc methods of Features class indicate if text and table extraction are supported
Supported Databases
GroupDocs.Parser works with any database that has a JDBC driver. Add the driver to the classpath of your application. Common drivers include:
SQLite - org.xerial:sqlite-jdbc
SQL Server - com.microsoft.sqlserver:mssql-jdbc
MySQL - com.mysql:mysql-connector-j
PostgreSQL - org.postgresql:postgresql
Oracle - com.oracle.database.jdbc:ojdbc11
Limitations
Database extraction is primarily for text-based table content
Complex relationships between tables are not automatically resolved
The list of tables is obtained from DatabaseMetaData.getTables with no type filter, so it contains every object the JDBC driver reports (depending on the driver, views and system tables as well)
Extract data with Connection object
To create an instance of Parser class to extract data from a database with java.sql.Connection object the following constructors are used:
The connection is owned by your code: GroupDocs.Parser doesn’t close it when the Parser object is closed. Close the connection yourself, for example with try-with-resources.
Step-by-Step Guide
Here are the steps to extract data from a database:
Call isText method to check if text extraction is supported;
Call isToc method to check if table of contents extraction is supported;
Call getToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
Complete Example: SQLite Database
The following example shows how to extract data from a SQLite database:
// Create java.sql.Connection object
try(java.sql.Connectionconnection=java.sql.DriverManager.getConnection(String.format("jdbc:sqlite:%s",Constants.SampleDatabase))){// Create an instance of Parser class to extract tables from the database
try(Parserparser=newParser(connection)){// Check if text extraction is supported
if(!parser.getFeatures().isText()){System.out.println("Text extraction isn't supported.");return;}// Check if toc extraction is supported
if(!parser.getFeatures().isToc()){System.out.println("Toc extraction isn't supported.");return;}// Get a list of tables
Iterable<TocItem>toc=parser.getToc();// Iterate over tables
for(TocItemi:toc){// Print the table name
System.out.println(i.getText());// Extract a table content as a text
try(TextReaderreader=parser.getText(i.getPageIndex())){System.out.println(reader.readToEnd());}}}}
Example: SQL Server Database
StringconnectionString="jdbc:sqlserver://localhost:1433;databaseName=MyDatabase;integratedSecurity=true;";try(java.sql.Connectionconnection=java.sql.DriverManager.getConnection(connectionString)){try(Parserparser=newParser(connection)){if(parser.getFeatures().isText()&&parser.getFeatures().isToc()){for(TocItemtable:parser.getToc()){System.out.println("Extracting from table: "+table.getText());try(TextReaderreader=parser.getText(table.getPageIndex())){if(reader!=null){System.out.println(reader.readToEnd());}}}}}}
Extract data with connection string
To create an instance of Parser class to extract data from a database with a JDBC connection string the following constructor is used:
Parser(StringfilePath,LoadOptionsloadOptions);
Parameters
filePath: The JDBC connection string (not a file path); it is passed to java.sql.DriverManager.getConnection
loadOptions: Must be initialized with FileFormat.Database
In this case GroupDocs.Parser opens the connections itself and closes them: the connection used to list the tables is closed right after that, and the connection used to read the table data is closed when the TextReader object is closed.
The list of tables is represented as table of contents. Each table can be extracted using the getText(int) method where the integer parameter is the table index from the table of contents.
Here are the steps to extract data from Sqlite database:
Call isText method to check if text extraction is supported;
Call isToc method to check if table of contents extraction is supported;
Call getToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
The following example shows how to extract data from Sqlite database:
StringconnectionString=String.format("jdbc:sqlite:%s",Constants.SampleDatabase);// Create an instance of Parser class to extract tables from the database
// As filePath connection parameters are passed; LoadOptions is set to Database file format
try(Parserparser=newParser(connectionString,newLoadOptions(FileFormat.Database))){// Check if text extraction is supported
if(!parser.getFeatures().isText()){System.out.println("Text extraction isn't supported.");return;}// Check if toc extraction is supported
if(!parser.getFeatures().isToc()){System.out.println("Toc extraction isn't supported.");return;}// Get a list of tables
Iterable<TocItem>toc=parser.getToc();// Iterate over tables
for(TocItemi:toc){// Print the table name
System.out.println(i.getText());// Extract a table content as a text
try(TextReaderreader=parser.getText(i.getPageIndex())){System.out.println(reader.readToEnd());}}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: