title: Need help with parsing sql in java
body: <p>I have the below code that splits the sql statement and give its indices for the columns. </p>
  
  <pre><code>String sql = "INSERT INTO Table1(SSN, EMPID) VALUES (?,?)";
  
      public static List&lt;Object[]&gt; indices(String stmt) {
          List&lt;Object[]&gt; list = new ArrayList&lt;Object[]&gt;();
          String variables[] = null;
          try {
              variables = stmt.substring(stmt.indexOf('(')+1, 
                  stmt.indexOf(')', stmt.indexOf('(')+1)).split("\\,");
          } catch (Exception e) {}
  
          for (int i=0; i &lt; variables.length; ++i ) {
              Object [] str =  new Object [2] ;
              str[0] = variables[i].trim() ;
              str[1] = ((Integer)(i+1)) ;
              list.add(str) ;
          }
          return list;
      }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>Result - 
  
  list[0] &gt;&gt;
  
  array[0] = SSN
  array[1] = 1
  
  list [1] &gt;&gt;
  array[0] = EMPID
  array[1] = 2
  </code></pre>
  
  <p>Can some one point me with appropriate regular expression to split the following sql instead -</p>
  
  <pre><code>sql = "if not exists (select * from Table1 where SSN = ? and EMPID =?)  
        INSERT INTO Table1(SSN, EMPID) VALUES (?,?)"
  </code></pre>
  
  <p>I guess the output would be something like -</p>
  
  <pre><code>list[0] &gt;&gt;
  array[0] = SSN
  array[1] = 1
  
  list [1] &gt;&gt;
  array[0] = EMPID
  array[1] = 2
  
  list[2] &gt;&gt;
  array[0] = SSN
  array[1] = 1
  
  list [3] &gt;&gt;
  array[0] = EMPID
  array[1] = 2
  </code></pre>
  
  <p>Thank You</p>