Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:18:26+00:00 2026-06-09T10:18:26+00:00

Do you know a java-function to split an INSERT statement INSERT INTO mytable (col1,

  • 0

Do you know a java-function to split an INSERT statement

INSERT INTO mytable (col1, col2, col3) VALUES ('val1', 'val2,andmore', 123)

into a map:

col1 => "val1"
col2 => "val2,andmore"
col3 => 123

(I don’t need the ESCAPE clause of Oracle for the time)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:18:28+00:00Added an answer on June 9, 2026 at 10:18 am

    I have found no solution but build my own function

    /**
     * The Unicode Char for "object replacement character"
     */
    private final static char REPLACEMENT_CHAR = '\uFFFC';
    
    /**
     * Split an SQL INSERT or UPDATE Statement to return the map of (column_name, values)
     * @param sql  the statement
     * @return the map with the column names as key and the sql values as values
     */
    public static final SortedMap<String, String> getValuesFromSqlStatement (String sql)
    {
        // check the argument
        if (sql==null || sql.trim().length() == 0)
            throw new IllegalArgumentException("The sql string cannot be empty");
        sql = sql.trim();
    
        final String ucSql = sql.toUpperCase();
        if (!ucSql.startsWith("INSERT") && !ucSql.startsWith("UPDATE"))
            throw new IllegalArgumentException("The sql string is neither an insert nor an update");
    
        // extract the column names
        Pattern pattern;
        pattern = Pattern.compile("[^(]*\\(([^)]*)\\).*");
        final Matcher m = pattern.matcher(sql);
        m.matches();
        final String columList = m.group(1);
        final String[] columns = columList.trim().split("\\s*,\\s*");
    
        // replace the , chars inside of strings by the REPLACEMENT_CHAR
        final char[] rest = sql.substring(sql.indexOf("VALUES")).toCharArray();
        boolean inString = false;
        int start = -1;
        int end   = -1;
        for (int i = 0; i<rest.length; i++)
        {
            final char c = rest[i];
            if ( inString && c == ',')
            {
                rest[i] = REPLACEMENT_CHAR;
            }
            else if ( c=='\'')
            {
                inString = !inString;
            }
            else if (!inString && c == '(')
            {
                if (start != -1)
                    throw new AssertionError("Start brace found twice!");
    
                start = i+1;
            }
            else if (!inString && c == ')')
            {
                end = i-1;
                break;
            }
        }
    
        if (start == -1 || end == -1)
            throw new AssertionError("Start or end of the values part not found!");
    
    
        // split the result for having the values
        final String[] values = new String(rest, start, end-start+1).split("\\s*,\\s*");
        if (values.length != columns.length)
            throw new AssertionError("The number of values differs of the number of columns");
    
        // reset the , char in the values containing REPLACEMENT_CHAR and collect the results
        final TreeMap<String, String> map = new TreeMap<String, String>();
        for (int i=0; i<values.length; i++)
        {
            if (values[i].indexOf(REPLACEMENT_CHAR) != -1)
            {
                values[i] = values[i].replace(REPLACEMENT_CHAR, ',');
            }
    
            map.put(columns[i], values[i]);
        }
    
        return map;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a split type function for C++ similar to Java? I know of
i know java API function charAt for example String s=dato char r=s.charAt(0); r is
do you know function in java that will validate a string to be a
I know that there is a function in Java to set Standard Output Stream
I know java well, and have some experience in EclipseLink, Hibernate, JSF, Grails and
I know Java's generics are somewhat inferior to .Net's. I have a generic class
I know Java doesn't allow unsigned types, so I was wondering how it casts
As we all know java allows us to use byte array as a buffer
As we all know Java program will start executing from the public static void
I'm currently getting to know Java and OSGi, so I've read a few books.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.