如何在不重复的情况下制作此代码? 这种方法很相似。
创建电话号码的方法:
private List<String> createPhoneNumbers(String sqlStatement, Long id) {
List<String> phones = new ArrayList<>();
try (PreparedStatement statement = this.connection.prepareStatement(sqlStatement)) {
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
phones.add(resultSet.getString("phoneNumber"));
}
return phones;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
创建好友列表的方法:
private List<Account> createFriendList(String sqlStatement, Long id) {
List<Account> friends = new ArrayList<>();
try (PreparedStatement statement = this.connection.prepareStatement(sqlStatement)) {
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
friends.add(createSimpleElement(resultSet));
}
return friends;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
创建组列表的方法:
private List<Group> createGroupList(String sqlStatement, Long id) {
List<Group> groups = new ArrayList<>();
try (PreparedStatement statement = this.connection.prepareStatement(sqlStatement)) {
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
groups.add(createGroupFromResultSet(resultSet));
}
return groups;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
这是我非常糟糕的解决方案。
//field values: 1- create String, 2 - createAccount, 3 - createGroup
private <E> List<E> createList(String selectSql, Long id, int field) {
List<E> list = new ArrayList<>();
try (PreparedStatement statement = this.connection.prepareStatement(selectSql)) {
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
switch (field) {
case 1:
list.add((E) resultSet.getString("phoneNumber"));
break;
case 2:
list.add((E) createSimpleElement(resultSet));
break;
case 3:
list.add((E) createGroupFromResultSet(resultSet));
break;
}
}
return list;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
我可以阅读哪些内容来解决代码中的类似问题?
答案 0 :(得分:2)
听起来我应该查看Spring JDBC及其基于回调的JdbcTemplate
方法。它将您的所有样板(数据库连接,事务,异常转换等)抽象出来,让您专注于应用程序代码。
样品:
List<Actor> actors = this.jdbcTemplate.query(
"select first_name, last_name from t_actor",
new RowMapper<Actor>() {
public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
});
答案 1 :(得分:1)
&#34;将哪些变化与保持相同的变化区分开来&#34; - 首创设计模式。
您可以将逻辑分离以从Resultset(更改)中提取数据,从准备语句和执行语句(不会更改)
如果您不想从任何框架支持中获取帮助,您可以执行以下操作
public interface GenericDao {
ResultSet find(String sqlStatement, Long id);
}
public class GenericDaoImpl extends GenericDao {
@Override
public ResultSet find(String sqlStatement, Long id) {
try (PreparedStatement statement = this.connection.prepareStatement(sqlStatement)) {
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
return resultSet;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
public interface ContactDao {
List<String> createPhoneNumbers(String sqlStatement, Long id);
List<Account> createFriendList(String sqlStatement, Long id);
List<Group> createGroupList(String sqlStatement, Long id);
}
public class ContactDaoImpl extends ContactDao{
private GenericDao genericDao;
ContactDaoImpl(GenericDao genericDao) {
this.genericDao = genericDao;
}
@Override
public List<String> createPhoneNumbers(String sqlStatement, Long id) {
ResultSet resultSet = this.genericDao.executeSqlQuery(sqlStatement, id);
while (resultSet.next()) {
phones.add(resultSet.getString("phoneNumber"));
}
return phones;
}
@Override
public List<Account> createFriendList(String sqlStatement, Long id) {
ResultSet resultSet = this.genericDao.executeSqlQuery(sqlStatement, id);
while (resultSet.next()) {
friends.add(createSimpleElement(resultSet));
}
return friends;
}
@Override
public List<Group> createGroupList(String sqlStatement, Long id) {
ResultSet resultSet = this.genericDao.executeSqlQuery(sqlStatement, id);
while (resultSet.next()) {
groups.add(createGroupFromResultSet(resultSet));
}
return groups;
}
}