jdbc连接好mysql数据库后给一张表根据关键字进行查询
1个回答
2015-06-28
展开全部
一般:
public List findByKey(String column ,String keyWord) throws SQLException{
List list = new ArrayList<TableX>(); //查询结果
String sql = "select * from TableX where "+column+" Like ?"; //构造sql
preparedStatement = conn.prepareStatement(sql); // Connection conn ; PreparedStatement preparedStatement ;
preparedStatement.setString(0, "%" + keyWord + "%");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) { //遍历所有数据,封装到domain对象TableX类
TableX tableX = new TableX();
tableX.setId(resultSet.getInt("id"));
list.add(tableX);
}
return list;
}
public List findByKey(String column ,String keyWord) throws SQLException{
List list = new ArrayList<TableX>(); //查询结果
String sql = "select * from TableX where "+column+" Like ?"; //构造sql
preparedStatement = conn.prepareStatement(sql); // Connection conn ; PreparedStatement preparedStatement ;
preparedStatement.setString(0, "%" + keyWord + "%");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) { //遍历所有数据,封装到domain对象TableX类
TableX tableX = new TableX();
tableX.setId(resultSet.getInt("id"));
list.add(tableX);
}
return list;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询