一.完善模糊查询
因为打的代码越来越多,为了后期看起来不会很乱,所以今天我们把seach方法删除了
其他改动的比较多
文件创建如下
实现效果如下
在搜索框里输入a,表里有a的都会显示出现
我把代码都贴在下面
IConsumerDao中代码
public interface IConsumerDao {
Consumer findConsumerByname(String name);
List<Consumer> findAll();
void delete(int id);
void add(String name,String password);
Consumer findConsumerById(int id);
void update(int id,String name,String password);
List<Consumer> findByPage(String name,int start,int size);
int selectCount();
}
ConsumerDaoImpl中代码
public class ConsumerDaoImpl implements IConsumerDao {
public Consumer findConsumerByname(String name) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
Consumer consumer = null;
try {
connection = DBUtil.getConnection();
String sql = "select * from consumer where name=?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
resultSet = statement.executeQuery();
while (resultSet.next()) {
consumer = new Consumer();
consumer.setId(resultSet.getInt(1));
consumer.setName(name);
consumer.setPassword(resultSet.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet, statement, connection);
}
return consumer;
}
@Override
public List<Consumer> findAll() {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<Consumer> consumers = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from consumer";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
Consumer consumer = new Consumer();
consumer.setId(resultSet.getInt(1));
consumer.setName(resultSet.getString(2));
consumer.setPassword(resultSet.getString(3));
consumers.add(consumer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet, statement, connection);
}
return consumers;
}
@Override
public void delete(int id) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
statement = connection.prepareStatement("delete from consumer where id=?");
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(null, statement, connection);
}
}
@Override
public void add(String name, String password) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
statement = connection.prepareStatement("insert into consumer(name,password) values (?,?)");
statement.setString(1, name);
statement.setString(2, password);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(nul