サーブレットでDAOを呼び出す書式を教えてください
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違っている場合はログイン出来ずエラーメッセージを表示させたいです
DAO
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class indexDAO {
private DataSource source;
private static final String SELECT = "select * from account where user_id = ? user_pass = ? ";
public indexDAO() throws NamingException {
InitialContext context = new InitialContext();
source = (DataSource) context.lookup("");
}
public List<AccountBean> getIndexList() throws SQLException {
List<AccountBean> indexList = new ArrayList<AccountBean>();
Connection connection = source.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1,"id");
statement.setString(2,"password");
ResultSet result = statement.executeQuery();
while (result.next()) {
AccountBean index = new AccountBean();
index.setId(result.getString("id"));
index.setPassword(result.getString("password"));
indexList.add(index);
}
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return indexList;
}
}
サーブレットのdopostメソッド
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("userID");
String password = request.getParameter("password");
ArrayList<String> errorList = new ArrayList<String>();
AccountBean account = new AccountBean();
DAO hoge = new DAO();
ArrayList<AccountBean> huga = hoge.select(AccountBean);
//エラーチェック処理
if (!Check.checkBlank(id) || !Check.checkBlank(password)) {
errorList.add("IDとパスワードは必須項目です。");
request.setAttribute("errorList", errorList);
String url = PropertyLoader.getProperty("url.jsp.index");
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
} else if (account != account) {
errorList.add("IDとパスワードが間違っています。");
request.setAttribute("errorList", errorList);
String url = PropertyLoader.getProperty("url.jsp.index");
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
} else {
String url = PropertyLoader.getProperty("url.sb.input");
response.sendRedirect(url);
}
}
}