{"id":1143,"date":"2022-07-22T16:54:15","date_gmt":"2022-07-22T11:24:15","guid":{"rendered":"https:\/\/techieshouts.com\/home\/?p=1143"},"modified":"2022-08-09T19:02:12","modified_gmt":"2022-08-09T13:32:12","slug":"java-parse-sql-select-query-using-jsqlparser","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/java-parse-sql-select-query-using-jsqlparser\/","title":{"rendered":"Java parse SQL Select query using JSQLParser"},"content":{"rendered":"\n<p>JSQLParser is one of the most powerful open source parsers for parsing SQL statements using Java classes. The parser is capable of parsing DDL and DML statements and exposing the details of the query.<\/p>\n\n\n\n<p>In this post, we will see how the JSQLParser library is used to parse a simple SELECT SQL statement and fetch the columns from that select statement.<\/p>\n\n\n\n<p>For using the parser, first, we need to add the dependency of the <a href=\"https:\/\/github.com\/JSQLParser\/JSqlParser\" target=\"_blank\" rel=\"noopener\">JSQLParser<\/a> library to our project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;dependency>\n            &lt;groupId>com.github.jsqlparser&lt;\/groupId>\n            &lt;artifactId>jsqlparser&lt;\/artifactId>\n            &lt;version>4.2&lt;\/version>\n&lt;\/dependency><\/pre>\n\n\n\n<h2>Parsing simple SELECT SQL with columns<\/h2>\n\n\n\n<p>In this example, let us take a simple select query that has columns selected from a single table under a database. Let&#8217;s try to parse this SQL and explore the objects inside the same.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"Simple SELECT SQL\" data-enlighter-group=\"\">SELECT\nID, NAME, CITY, ADDRESS, COUNTRY, GENDER, CREATED_DT\nFROM CUSTOMERDB.CUSTOMERINFO <\/pre>\n\n\n\n<p>The above SQL is a simple one with a few columns selected from the CUSTOMERINFO table which is in the CUSTOMERDB database. Let us explore how to parse this.<\/p>\n\n\n\n<p>After adding this dependency, you will be able to use the classes from the JSQLParser library.<\/p>\n\n\n\n<p>Let us extract all the columns from the select query and print. The following JSQLParser example will show how to parse a simple select query in Java.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String selectSQL = \"SELECT\\n\" +\n                \"ID, NAME, CITY, ADDRESS, COUNTRY, GENDER, CREATED_DT\\n\" +\n                \"FROM CUSTOMERDB.CUSTOMERINFO \";\n\nStatement select = (Statement) CCJSqlParserUtil.parse(selectSQL);\nList&lt;SelectItem> selectlist = ((PlainSelect) ((Select) select).getSelectBody()).getSelectItems();\n\nSystem.out.println(\"List of  columns in select query\");\nSystem.out.println(\"--------------------------------\");\nList&lt;SelectItem> selectCols = ((PlainSelect) ((Select) select).getSelectBody()).getSelectItems();\n\nfor (SelectItem selectItem : selectCols)\n   System.out.println(selectItem.toString());<\/pre>\n\n\n\n<h2><meta charset=\"utf-8\">Parsing simple SELECT SQL with Where clause<\/h2>\n\n\n\n<p>Let us try to parse another SQL statement with a where condition. Along with extracting the columns from the select statement, we can extract the where condition also.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String selectSQL = \"SELECT\\n\" +\n                \"ID, NAME, CITY, ADDRESS, COUNTRY, GENDER, CREATED_DT\\n\" +\n                \"FROM CUSTOMERDB.CUSTOMERINFO WHERE CITY='CHENNAI'\";\n\nStatement select = (Statement) CCJSqlParserUtil.parse(selectSQL);\nString whereCondition = ((PlainSelect) ((Select) select).getSelectBody()).getWhere().toString();\nSystem.out.println(\"Where condition: \" + whereCondition);<\/pre>\n\n\n\n<p>From the above code, you can see that we are first extracting the Select object from the SQL, and in that, we are able to extract the where condition.<\/p>\n\n\n\n<h2><meta charset=\"utf-8\">Parsing SELECT SQL with Join conditions<\/h2>\n\n\n\n<p>In this example, let us try to parse a select statement that has multiple join statements. We will parse the join statements and extract the info like the number of joins used, what are the expressions used in each of the ON conditions of the statement.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String selectSQL = \"SELECT CI.ID, CI.NAME, C.COUNTRY, S.SALESDATE \\n\" +\n                \"FROM CUSTOMERDB.CUSTOMERINFO CI\\n\" +\n                \"LEFT JOIN CUSTOMERDB.COUNTRY C ON CI.COUNTRYCODE = C.COUNTRYCODE  \" +\n                \"LEFT JOIN CUSTOMERDB.SALES S ON CI.COUNTRYCODE = S.COUNTRYCODE;\";\n\nStatement select = (Statement) CCJSqlParserUtil.parse(selectSQL);\nList&lt;Join> joins = ((PlainSelect) ((Select) select).getSelectBody()).getJoins();\nSystem.out.println(\"Number of joins: \" + joins.size());\nfor(Join join : joins)\n{\n    System.out.println(\"Join condition: \" + join.toString());\n    System.out.println(\"ON elements of join\");\n\n    for (Expression exp: join.getOnExpressions())\n          System.out.println(exp.toString());\n}<\/pre>\n\n\n\n<p>As mentioned, there are two join conditions in the above SQL statement. The first for loop will process each join statement. The second for loop will go through each ON expression of that join condition.<\/p>\n\n\n\n<p><meta charset=\"utf-8\">Also check &#8220;<a href=\"https:\/\/techieshouts.com\/parsing-sql-insert-query-using-jsqlparser-in-java\/\">Parsing INSERT sql using jsqlparser<\/a>&#8220;,&#8221;<a href=\"https:\/\/techieshouts.com\/parsing-sql-create-query-using-jsql-parser\/\">Parsing CREATE sql using jsqlparser<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSQLParser is one of the most powerful open source parsers for parsing SQL statements using Java classes. The parser is capable of parsing DDL and DML statements and exposing the details of the query. In this post, we will see how the JSQLParser library is used to parse a simple SELECT SQL statement and fetch\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/java-parse-sql-select-query-using-jsqlparser\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,14],"tags":[195,190,237,193,192,194],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1143"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=1143"}],"version-history":[{"count":12,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1143\/revisions"}],"predecessor-version":[{"id":1187,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1143\/revisions\/1187"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=1143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=1143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=1143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}