标签:列表 arraylist amp content dstat des print connect auto
//根据查询条件查询消息列表
public List<Message> queryMessageList(String command,String description){
List<Message> messageList = new ArrayList<Message>();
try {
Class.forName("com.mysql.jdbc.Driver");//1.加载数据库驱动
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/micro_message", "root", "root");//2.获取连接对象
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1");
List<String> paramList = new ArrayList<String>();
//拼接sql语句
if(command!=null&&!"".equals(command.trim())){
sql.append(" and COMMAND=?");
paramList.add(command);
}
if(description!=null&&!"".equals(description.trim())){
sql.append(" and DESCRIPTION like ‘%‘ ? ‘%‘");//mysql字符串拼接用空格拼,传进占位符处的参数和%拼接到一块
paramList.add(description);
}
//3.预编译SQL语句
PreparedStatement statement = conn.prepareStatement(sql.toString());
//4.填充占位符
for(int i = 0;i < paramList.size(); i++){
statement.setString(i+1,paramList.get(i));
}
//5.查询数据库
ResultSet rs = statement.executeQuery();
//6.遍历出所有数据
while(rs.next()){
Message message = new Message();
messageList.add(message);
message.setId(rs.getString("ID"));
message.setCommand(rs.getString("COMMAND"));
message.setDescription(rs.getString("DESCRIPTION"));
message.setContent(rs.getString("CONTENT"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}
return messageList;
}
标签:列表 arraylist amp content dstat des print connect auto
原文地址:https://www.cnblogs.com/wangxiaochao/p/9314108.html