[英]Deduct one character until a row is returned, and trim spaces when returning values [Java/SQL]
i need to search string in SQL using Java, expectation is something like this:我需要使用 Java 在 SQL 中搜索字符串,期望是这样的:
input: 123456
输入: 123456
data in SQL to be fetched: 123777
要获取的 SQL 中的数据: 123777
result is: ' AB C '
结果是: ' AB C '
result should be: 'ABC'
结果应该是: 'ABC'
iteration will be like:迭代将类似于:
select col1, col2, col3 from table where input like '123456%'; --no row returned
select col1, col2, col3 from table where input like '12345%'; --no row returned
select col1, col2, col3 from table where input like '1234%'; --no row returned
select col1, col2, col3 from table where input like '123%'; --returns row for 123777
this is my current code:这是我当前的代码:
public Output Method (String input) throws exception{
Connection connection = getSQLConnection();
String SQLquery = "SELECT COL1, COL2, COL3 FROM TABLE WHERE INPUT LIKE ?";
if (connection != null){
PreparedStatement ps = connection.prepareStatement(SQLquery);
ps.setString(1, input + "%");
ResultSet rs = ps.executeQuery();
// how to deduct characters until a match is found?
logger.debug("Executed: "+SQLquery+"; input => ["+input+"]");
if(rs.next()){
output = new Output();
output.setOut1(rs.getString(1));
output.setOut2(rs.getString(2));
output.setOut2(rs.getString(3));
//how to remove all spaces from in per result?
//sample result: ' AB C ' -> should be 'ABC'
}else{
logger.debug("no row returned");
}
}
}
for deduct character, this should be done:对于扣除字符,应该这样做:
for(int i = 0; i < input.length(); i++){
ps.setString(1, input.substring(0, input.length()-i));
}
for trim spaces, it should be:对于修剪空间,它应该是:
output.setOut1(rs.getString(1).replaceAll("\\s", ""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.