Java sql删除括号
这是我收到的输出:
INSERT INTO [ J] VALUES ([J1, Sorter, Paris)
INSERT INTO [ J] VALUES (J2, Punch, Rome)
INSERT INTO [ J] VALUES (J3, Reader, Athens)
INSERT INTO [ J] VALUES (J4, Console, Athens)
INSERT INTO [ J] VALUES (J5, Collator, London)
INSERT INTO [ J] VALUES (J6, Termninal, Oslo)
INSERT INTO [ J] VALUES (J7, Tape, London])
正如你所看到的,我在开头和结尾都有一个,我不知道是什么原因造成的?我怎样才能删除它?我用它试过regex。替换(“[]”,“”);我相信问题正在发生,但没有结果
插入代码:
if (state == 3||!tableLineScanner.hasNextLine()){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:sqlite:test.db");
//TODO Input values goes here
tableName =""+tableNames;
Statement stmt = conn.createStatement();
String query = tableValues + "";
ResultSet rs = stmt.executeQuery("SELECT * FROM "+ tableName);
ResultSetMetaData rsmd = rs.getMetaData();
query.replaceAll("[ ]","");
tableName =""+tableNames;
tableName.replaceAll("[ ]","");
String[] splittedOutput = query.split(", ");
int valuesOnLine = rsmd.getColumnCount();
for (int i = 0; i < splittedOutput.length; i++) {
if (i % valuesOnLine == 0) {
System.out.print("INSERT INTO " + tableName + " VALUES (");
}
System.out.print(splittedOutput[i]);
if (i % valuesOnLine == valuesOnLine - 1) {
System.out.println(")");
} else {
System.out.print(", ");
}
}
tableNames.clear();
tableValues.clear();
tableFields.clear();
}
catch (SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("VendorError: " + ex.getErrorCode());
}
catch (ClassNotFoundException e){
e.printStackTrace();
}
}
# 1 楼答案
如果在
tableNames
中有多个元素,您将遇到更多问题。是否仅使用第一个表名?或者使用所有这些元素在tableNames
中进行迭代?现在,您正在将tableNames
转换为形式为[element1, element2, element3]
的字符串,这几乎肯定不是您想要的但是,如果您确实想删除
[]
字符,则需要编写类似replaceAll("[\\[\\]]", "")
的代码,因为[]
是正则表达式中的特殊字符# 2 楼答案
删除括号的解决方案是通过将splittedOutput配置为: