我正在尝试将元组插入到我为SQL构建的数据库模式的新创建的表中。
问题是,我希望第一行是
ssn INTEGER(9), cname VARCHAR(25), gender VARCHAR(6), age VARCHAR(3), profession VARCHAR(25)
但我希望它只是这样:
ssn, cname, gender, age, profession
前面的方法我尝试了两个分割,一个用于空间而另一个用于逗号不起作用,所以我认为使用replace all会更容易。但是,我不确定要为正则表达式尝试什么。应该如何创建?
private static String parseFile (String[] x, Connection conn,
String tableName) {
// assume the first line is the relation name layout
String query = "INSERT INTO " + tableName;
String firstLine = x[0];
//System.out.println(firstLine);
String[] splits = firstLine.split(" ");
String[] finalSplit = new String[50];
String finalString = "";
for (int i=0; i<splits.length; i++) {
int counter = 0;
String[] split2 = splits[i].split(",");
//System.out.println (splits[i]);
for (int j=0; j<split2.length; j++) {
finalSplit[j+counter] = split2[j];
//System.out.println (split2[j]);
if (j%2 == 0)
finalString += split2[j];
counter += 1;
}
} // end outside for
System.out.println ("The attribute string is: " + finalString);
for (int i=1 ; i<x.length; i++)
{
String line = x[i];
String Final = query + " " + finalString + " " + line;
System.out.println ("Final string: " + Final);
}
return finalString;
}
我在此感谢您的一些指导。
编辑: 一些输出是:
The attribute string is: ssnINTEGER(9)cnameVARCHAR(25)genderVARCHAR(6)ageVARCHAR(3)professionVARCHAR(25)
Final string: INSERT INTO customer ssnINTEGER(9)cnameVARCHAR(25)genderVARCHAR(6)ageVARCHAR(3)professionVARCHAR(25) 3648993,Emily,male,63,Consulting
Final string: INSERT INTO customer ssnINTEGER(9)cnameVARCHAR(25)genderVARCHAR(6)ageVARCHAR(3)professionVARCHAR(25) 5022334,Barbara,male,26,Finance
Final string: INSERT INTO customer ssnINTEGER(9)cnameVARCHAR(25)genderVARCHAR(6)ageVARCHAR(3)professionVARCHAR(25) 1937686,Tao,female,5,IT
x的一些输入是:
ssn INTEGER(9), cname VARCHAR(25), gender VARCHAR(6), age VARCHAR(3), profession VARCHAR(25)
3648993,Emily,male,63,Consulting
5022334,Barbara,male,26,Finance
1937686,Tao,female,5,IT
答案 0 :(得分:0)
尝试
firstLine.replaceAll(" [A-Z]+\\(\\d+\\)","");
说明:此正则表达式查找具有1个或更多大写字母的单词,后面紧跟左括号,一个或多个数字,右括号和逗号。
replaceAll
用空字符串替换所有实例。