使用MVC模式开发程序,完成数据的模糊查询(4)
ArrayList arrayList = qd.queryLike(name);
request.setAttribute("arrayList", arrayList);
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
[java]
package com.mars;
import java.sql.*;
import java.util.ArrayList;
import com.mars.Student;
public class QueryDAO {
private Connection conn = null;
public QueryDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testmvc", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public ArrayList queryLike(String name) {
ArrayList arrayList = new ArrayList();
String SQL = "select * from student where name like" + "'%" + name
+ "%'" + "or address like " + "'%" + name + "%'";
try {
ResultSet resultSet = conn.createStatement().executeQuery(SQL);
while (resultSet.next()) {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
相关新闻>>
- 发表评论
-
- 最新评论 更多>>