Project scenario:
tip: briefly describe the project background here:
for example, project scenario: example: communicate with mobile app through Bluetooth chip (hc-05), and transmit a batch of sensor data every 5S (not very large)
Problem Description:
The following code will report an error if it can’t be found in the database, and it doesn’t work whether you judge it to be empty or judge the length.
@Override
public String queryUserNameByUserId(String userId) {
String sql = "SELECT username FROM info WHERE userId= :userId";
MapSqlParameterSource source = new MapSqlParameterSource();
source.addValue("userId", userId);
return namedParameterJdbcTemplate.queryForObject(sql, source, String.class);
}
Cause analysis:
Queryforobject(), try to operate when querying data that must exist in the database.
Solution:
1. Try catch is not recommended
2. Change to list and judge to be empty, as follows.
@Override
public List<String> queryUserNameByUserId(String userId) { //Modify
String sql = "SELECT username FROM info WHERE userId= :userId";
MapSqlParameterSource source = new MapSqlParameterSource();
source.addValue("userId", userId);
return namedParameterJdbcTemplate.queryForList(sql, source, String.class); //修改
}
Read More:
- How to Solve Maven Error: Return code is: 501 , ReasonPhrase:HTTPS Required.
- How to Fix Error return arrays must be of arraytype
- How to Solve LeetCode Error: AddressSanitizer:DEADLYSIGNAL
- How to Solve Error: Type mismatch: cannot convert from Object to Car
- How to Solve Jetpack room use error
- How to Solve Spring Cloud Error context has been closed already
- How to Solve Hyperf Failed to Start Error After Aliyun ACM Installed
- Android 10: How to Solve Systemui Error
- How to Solve Pycharm SQL Union Error
- How to Solve error C2039: “to_ String “: not a member of” STD “
- How to Solve Starrocks Various Error
- How to Solve Logstash error: failed to execute action
- Vue: How to Solve Error uncaught (in promise) cancel
- How to Solve DB2 uses Limit Error
- How to Solve [error] malformed \uxxxx encoding Error
- How to Solve Error: Unsupported field: HourOfDay
- How to Solve forEach cannot exit the loop Issue
- How to Solve Android Error: gps requires ACCESS_FINE_LOCATION
- How to Solve “Error db.num is null” when starting Nacos