如何从字符串中单独获取表名,列名和列值

时间:2016-02-19 07:32:03

标签: java arrays string stringbuilder stringbuffer

我有如下的字符串

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

如何从字符串

中分别获取表名,列名和值

2 个答案:

答案 0 :(得分:1)

假设您对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

输出:

varchar

准备Update语句可能有点棘手,因为您必须考虑是设置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

输出:

Route::group(['middleware'=>'auth'], function(){
    Route::get('backend/dashboard', array('as'=>'dashboard', 'uses'=>'BackendDashboardController@getDashboard'));
});

您可以更多地优化代码。这只是为了说明目的。

答案 1 :(得分:1)

第一种方法,用regexp拆分字符串。所需的字符串将位于idex 2(表名),索引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