有没有办法从拆分函数中获得一致的结果?(相同数量的单词)
Is there a way to make consistent result out of split function?(same amounts of words)
我正在研究搜索功能,我正在将搜索查询拆分为单独的词。我知道不会超过10个字,但可能会更少
由于我需要进行 SQL 查询,并且字数保持一致,比如说 10,我需要再添加 8 个空白词,以防用户只输入 2 个。
有什么聪明的方法可以做到这一点,还是我应该从 1 到 10 循环并检查该特定位置是否为空,如果是则用空白词填充它?
SQL 查询看起来像这样(但有 9 个 AND):
SELECT * FROM mytable
WHERE column1 LIKE '%word1%'
AND column1 LIKE '%word2%'
AND column1 LIKE '%word3%'
基本上我想做的是这个,但也许以更聪明的方式:
String[] finalSearch = new String[10];
String[] search=searchInput.split(" ", -1);
for (int i=0; i<10; i++){
if (search[i]!=null){
finalSearch[i] = search[i];
}else{
finalSearch[i]="";
}
}
假设您有一个列表,其中包含您要搜索的项:
// List<String> terms
StringBuilder sqlQuery = new StringBuilder("SELECT * FROM mytable ");
if (terms.isEmpty()) { throw new IllegalArgumentException("SOME DESCRIPTION HERE") }
sqlQuery.append("WHERE column1 LIKE ? ");
for (int i = 1; i < terms.length(), i++) {
sqlQuery.append("AND column1 LIKE ? ");
}
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
// Get your JDBC connection as usual
// connection = getJdbcConnection();
preparedStatement = connection.prepareStatement(sqlQuery.toString());
for (int i = 0; i < terms.length(), i++) {
preparedStatement.setString(i + 1, "%" + terms[i] + "%");
}
} catch (SQLException e) {
// Do something here with he exception
} finally {
// Make sure you close JDBC resources here
}
您可以像这样根据用户输入生成查询:
static String getQuery(String ... userInput){
StringBuilder sb=new StringBuilder("SELECT * FROM mytable");
if (userInput.length>0){
sb.append(" where");
for (int i=0;i<userInput.length;i++){
sb.append(" column1 LIKE '%"+userInput[i]+"%'");
if (i!=userInput.length-1) sb.append(" AND");
}
}
sb.append(";");
return sb.toString();
}
您可以让它仅使用您需要的词来构建查询,而不是使用固定数量的词 (10) 吗?
你能做这样的事情吗:
String [] words = search.split ();
query = "SELECT * FROM my table WHERE";
for (int i = 0; i < words.length; i++){
query += "columns LIKE " + words[i];
If (i < words.length - 1)
query += "AND";
}
我正在研究搜索功能,我正在将搜索查询拆分为单独的词。我知道不会超过10个字,但可能会更少
由于我需要进行 SQL 查询,并且字数保持一致,比如说 10,我需要再添加 8 个空白词,以防用户只输入 2 个。
有什么聪明的方法可以做到这一点,还是我应该从 1 到 10 循环并检查该特定位置是否为空,如果是则用空白词填充它?
SQL 查询看起来像这样(但有 9 个 AND):
SELECT * FROM mytable
WHERE column1 LIKE '%word1%'
AND column1 LIKE '%word2%'
AND column1 LIKE '%word3%'
基本上我想做的是这个,但也许以更聪明的方式:
String[] finalSearch = new String[10];
String[] search=searchInput.split(" ", -1);
for (int i=0; i<10; i++){
if (search[i]!=null){
finalSearch[i] = search[i];
}else{
finalSearch[i]="";
}
}
假设您有一个列表,其中包含您要搜索的项:
// List<String> terms
StringBuilder sqlQuery = new StringBuilder("SELECT * FROM mytable ");
if (terms.isEmpty()) { throw new IllegalArgumentException("SOME DESCRIPTION HERE") }
sqlQuery.append("WHERE column1 LIKE ? ");
for (int i = 1; i < terms.length(), i++) {
sqlQuery.append("AND column1 LIKE ? ");
}
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
// Get your JDBC connection as usual
// connection = getJdbcConnection();
preparedStatement = connection.prepareStatement(sqlQuery.toString());
for (int i = 0; i < terms.length(), i++) {
preparedStatement.setString(i + 1, "%" + terms[i] + "%");
}
} catch (SQLException e) {
// Do something here with he exception
} finally {
// Make sure you close JDBC resources here
}
您可以像这样根据用户输入生成查询:
static String getQuery(String ... userInput){
StringBuilder sb=new StringBuilder("SELECT * FROM mytable");
if (userInput.length>0){
sb.append(" where");
for (int i=0;i<userInput.length;i++){
sb.append(" column1 LIKE '%"+userInput[i]+"%'");
if (i!=userInput.length-1) sb.append(" AND");
}
}
sb.append(";");
return sb.toString();
}
您可以让它仅使用您需要的词来构建查询,而不是使用固定数量的词 (10) 吗?
你能做这样的事情吗:
String [] words = search.split ();
query = "SELECT * FROM my table WHERE";
for (int i = 0; i < words.length; i++){
query += "columns LIKE " + words[i];
If (i < words.length - 1)
query += "AND";
}