如何从字符串中分别获取表名、列名和列值

How to get tablename,column names and column values seperatly from string

我有如下所示的字符串

 String stat="INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";

如何从字符串

中获取table名称、列名称和值

假设您的 stat 字符串遵循相同的格式,您可以按照以下方式进行操作:

public static void main (String[] args) 
{
    String stat = "INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";
    System.out.println("Table Name: " + stat.substring(stat.indexOf("INSERT INTO ") + 12,stat.indexOf("(")));
    System.out.println("Column Names: " + stat.substring(stat.indexOf("(") + 1,stat.indexOf(")")));
    System.out.println("Column Values: " + stat.substring(stat.indexOf("VALUES (") + 8,stat.lastIndexOf(")")));
}

输出:

Table Name: DEPARTMENT
Column Names: DNO, NAME, TEST_ID
Column Values: 2, 'ADM', 1

准备 Update 语句可能有点棘手,因为您必须注意是设置 varchar 还是 int 并根据数据类型进行更改。例如,您将始终用单引号将 varchar 数据括起来。

下面是关于如何准备更新语句的说明:

public static void main (String[] args) 
{
    String stat = "INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";
    String tableName = stat.substring(stat.indexOf("INSERT INTO ") + 12,stat.indexOf("("));
    String columnNames = stat.substring(stat.indexOf("(") + 1,stat.indexOf(")"));
    String columnValues = stat.substring(stat.indexOf("VALUES (") + 8,stat.lastIndexOf(")"));

    /* Prepare New Values Object */
    Object[] newValues = new Object[3];
    newValues[0] = new Integer(1);
    newValues[1] = new String("foo");
    newValues[2] = new Integer(8);

    /* Extract Column Names & Appen New Values */
    StringBuilder sb = new StringBuilder();
    sb.append("UPDATE " + tableName + " SET ");
    String[] splits = columnNames.split(", ");
    for(int i = 0; i < splits.length; i++) {
        sb.append(newValues[i] instanceof String ? splits[i] + " = '" + newValues[i] + "', " : splits[i] + " = " + newValues[i] + ", ");
    }
    String newString = sb.substring(0, sb.length() - 2);

    /* Extract Old Values & Prepare Where Caluse */
    sb = new StringBuilder();
    sb.append(" WHERE ");
    String[] splitz = columnValues.split(", ");
    for(int i = 0; i < splits.length; i++) {
        sb.append(splits[i] + " = " + splitz[i] + " AND ");
    }
    String where = sb.substring(0, sb.length() - 5);

    /* Print Result */
    System.out.println(newString + where);
}

输出:

UPDATE DEPARTMENT SET DNO = 1, NAME = 'foo', TEST_ID = 8 WHERE DNO = 2 AND NAME = 'ADM' AND TEST_ID = 1

您可以进一步优化代码。这仅用于说明目的。

第一种方法,用正则表达式拆分字符串。所需的字符串将位于索引 2(table 名称)、索引 3(列名称)和索引 5(值):

的数组中
public static void main(String[] args){        

    String stat="INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";
    String[] strings = stat.split("[\(\)]|(\b\s\b)");

    System.out.println(strings[2]);
    System.out.println(strings[3]);
    System.out.println(strings[5]);
}

输出将是:

DEPARTMENT
DNO, NAME, TEST_ID
2, 'ADM', 1

要得到单独的列名,可以再次拆分相应的字符串:

public static void main(String[] args){        

    String stat="INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";
    String[] strings = stat.split("[\(\)]|(\b\s\b)");

    String[] columnNames = (strings[3].split(",\s"));
    for(String s:columnNames){
        System.out.println(s);
    }
}

输出将是:

DNO
NAME
TEST_ID

您可以用同样的方式处理带有值的字符串。这种方法代码较少。

第二种方法(我更喜欢)是简单的正则表达式:

publi static main(String[]args) {

    String stat="INSERT INTO DEPARTMENT(DNO, NAME, TEST_ID) VALUES (2, 'ADM', 1)";
    Pattern pattern = Pattern.compile("INSERT INTO (\w+)\((.*)\) VALUES \((.*)\)");
    Matcher matcher = pattern.matcher(stat);
    if(matcher.find()){
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
    }
}

输出将是:

DEPARTMENT
DNO, NAME, TEST_ID
2, 'ADM', 1