Skip to content

Quick Start

Get up and running with General SQL Parser in just a few minutes! This guide will walk you through installation, basic setup, and your first SQL parsing example.

Prerequisites

  • Java 8 or higher (Java 11+ recommended)
  • Maven or Gradle for dependency management
  • IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)

Installation

  1. Download the trial version JAR file from our website
  2. Extract the downloaded ZIP file (e.g., gudusoft.gsqlparser.trial.3.0.9.0.zip)
  3. Navigate to the extracted directory containing the JAR file (e.g., gsqlparser-3.0.9.0.jar)

Maven

  1. Install the JAR into your local Maven repository using one of the mvn install:install-file commands shown below
1
2
3
4
5
6
mvn install:install-file \
-Dfile=gsqlparser-3.0.9.0.jar \
-DgroupId=gudusoft \
-DartifactId=gsqlparser \
-Dversion=3.0.9.0 \
-Dpackaging=jar
1
2
3
4
5
6
mvn install:install-file ^
-Dfile=gsqlparser-3.0.9.0.jar ^
-DgroupId=gudusoft ^
-DartifactId=gsqlparser ^
-Dversion=3.0.9.0 ^
-Dpackaging=jar  
  1. The installed JAR file will be located in your local Maven repository, typically at:

JAR file location

  • Linux/macOS: ~/.m2/repository/gudusoft/gsqlparser/3.0.9.0/
  • Windows: C:\Users\<username>\.m2\repository\gudusoft\gsqlparser\3.0.9.0\

Version Number

Please replace the version number "3.0.9.0" in the commands above with the actual version number of General SQL Parser Java that you downloaded from www.sqlparser.com. The version number can be found in the downloaded ZIP file name or JAR file name.

  1. Add the General SQL Parser dependency to your pom.xml:
1
2
3
4
5
<dependency>
    <groupId>gudusoft</groupId>
    <artifactId>gsqlparser</artifactId>
    <version>3.0.9.0</version>
</dependency>
  1. Navigate to your project directory containing pom.xml and compile the project:

    1
    mvn compile
    

  2. To run your program, you'll need to configure the main class in your pom.xml. Add this plugin configuration:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <mainClass>GSPDemo</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    Then run:
    1
    mvn exec:java
    

Manual Installation

  1. Download the JAR file from our website
  2. Add it to your project's classpath
  3. Include any required dependencies

Your First SQL Parser

Let's create a simple example that parses a SQL statement:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.EDbVendor;

public class QuickStartExample {
    public static void main(String[] args) {
        // Create a parser instance for Oracle SQL
        TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);

        // Set the SQL text to parse
        sqlparser.sqltext = "SELECT employee_id, first_name, last_name " +
                           "FROM employees " +
                           "WHERE department_id = 10 " +
                           "ORDER BY last_name";

        // Parse the SQL
        int result = sqlparser.parse();

        if (result == 0) {
            System.out.println("✅ SQL parsed successfully!");

            // Get basic information about the parsed SQL
            System.out.println("SQL Type: " + sqlparser.sqlstatements.get(0).sqlstatementtype);
            System.out.println("Number of statements: " + sqlparser.sqlstatements.size());

        } else {
            System.out.println("❌ Parse failed!");
            System.out.println("Error: " + sqlparser.getErrormessage());
        }
    }
}

Expected Output

1
2
3
✅ SQL parsed successfully!
SQL Type: sstselect
Number of statements: 1

Database Vendor Support

General SQL Parser supports 30+ database vendors. Here are some common examples:

1
2
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
parser.sqltext = "SELECT * FROM dual WHERE ROWNUM = 1";
1
2
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvmssql);
parser.sqltext = "SELECT TOP 10 * FROM employees";
1
2
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvpostgresql);
parser.sqltext = "SELECT * FROM employees LIMIT 10";
1
2
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvmysql);
parser.sqltext = "SELECT * FROM employees LIMIT 10";
1
2
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvbigquery);
parser.sqltext = "SELECT * FROM `project.dataset.table` LIMIT 10";

Common Use Cases

1. SQL Validation

1
2
3
4
5
public boolean isValidSQL(String sql, EDbVendor vendor) {
    TGSqlParser parser = new TGSqlParser(vendor);
    parser.sqltext = sql;
    return parser.parse() == 0;
}

2. Extract Table Names

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
import gudusoft.gsqlparser.nodes.TTable;

public void extractTables(String sql) {
    TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
    parser.sqltext = sql;

    if (parser.parse() == 0) {
        TSelectSqlStatement select = (TSelectSqlStatement) parser.sqlstatements.get(0);

        for (int i = 0; i < select.tables.size(); i++) {
            TTable table = select.tables.getTable(i);
            System.out.println("Table: " + table.getTableName());
        }
    }
}

3. Format SQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.TGSqlParser;

import gudusoft.gsqlparser.pp.para.GFmtOptFactory;
import gudusoft.gsqlparser.pp.para.GFmtOpt;
import gudusoft.gsqlparser.pp.stmtformatter.FormatterFactory;


public class formatsql {

    public static void main(String args[])
     {

        TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvpostgresql);

        sqlparser.sqltext ="WITH upd AS (\n" +
                "  UPDATE employees SET sales_count = sales_count + 1 WHERE id =\n" +
                "    (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')\n" +
                "    RETURNING *\n" +
                ")\n" +
                "INSERT INTO employees_log SELECT *, current_timestamp FROM upd;";



        int ret = sqlparser.parse();
        if (ret == 0){
            GFmtOpt option = GFmtOptFactory.newInstance();
            String result = FormatterFactory.pp(sqlparser, option);
            System.out.println(result);
        }else{
            System.out.println(sqlparser.getErrormessage());
        }
     }
}

Error Handling

Always handle parsing errors gracefully:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.TSyntaxError;

public class QuickStartExample {
    public static void main(String[] args) {
        TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
        parser.sqltext = "SELECT * FROM"; // Invalid SQL

        int result = parser.parse();
        if (result != 0) {
            System.err.println("Parse Error:");
            System.err.println("Message: " + parser.getErrormessage());
            for(TSyntaxError error : parser.getSyntaxErrors()) {
                System.err.println("Line: " + error.lineNo);
                System.err.println("Column: " + error.columnNo);
                System.err.println("Error: " + error.tokentext);
                System.err.println("Expected: " + error.errortype);
            }
        }
    }
}

Next Steps

Now that you have General SQL Parser running, explore these areas:

Continue Learning

Troubleshooting

Common Issues

Parse Error: Unexpected token

Solution: Check that you're using the correct database vendor. SQL syntax varies between databases.

1
2
3
4
5
6
7
// Wrong
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
parser.sqltext = "SELECT TOP 10 * FROM table"; // SQL Server syntax

// Correct
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvmssql);
parser.sqltext = "SELECT TOP 10 * FROM table";

ClassNotFoundException

Solution: Ensure the GSQLParser JAR is in your classpath and all dependencies are included.

OutOfMemoryError

Solution: For large SQL files, consider parsing statements individually or increase JVM heap size.

Getting Help

  • 📖 Check our FAQ for common questions
  • 💬 Visit our Support page for community help
  • 📧 Contact technical support for commercial licenses

Ready for more advanced features? Continue with our comprehensive tutorials or explore specific how-to guides.