请我对我的 ConnectLogin 和我的 ServletValidLogin 有一些疑问:我的 ConnectLogin
package br.com.cad.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ConnectLogin extends ConnectDb {
public Contato getContato( String email, String senha ){
Connection c = this.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = c.prepareStatement("select pf_email, pf_senha from dados_cadastro where pf_email = ? and pf_senha = ?");
ps.setString(1, email);
ps.setString(2, senha);
rs = ps.executeQuery();
if ( rs.next() ){
Contato user = new Contato();
user.setEmail(email);
user.setSenha(senha);
user.setNome( rs.getString("pf_nome") );
return user;
}
}
catch (SQLException e){
e.printStackTrace();
}
finally{
if (rs != null ) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (ps != null ) {
try { ps.close(); } catch (SQLException e) { ; }
ps = null;
}
if (c != null ) {
try { c.close(); } catch (SQLException e) { ; }
c = null;
}
}
return null;
}
}
我的小服务程序:
package br.com.cad.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import br.com.cad.dao.ConnectLogin;
import br.com.cad.basica.Contato;
public class ServletValidaLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
HttpSession session = request.getSession();
Contato user = null;
String email = request.getParameter("email");
String senha = request.getParameter("password");
try {
ConnectLogin dao = new ConnectLogin();
user = dao.getContato(email, senha);
}
catch ( Exception e ){
}
if ( user == null ) {
session.invalidate();
request.setAttribute("msg", "Usuário ou senha inválidos");
request.getRequestDispatcher("login.jsp" ).forward(request, response);
}
else{
session.setAttribute("user", user);
request.getRequestDispatcher("home.jsp" ).forward(request, response);
}
}
}
还有我的 ConnectDb:
package br.com.cad.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectDb {
public Connection getConnection() {
try {
System.out.println("Connect to database...");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/soa", "root", "wey123");
} catch(SQLException sqlException) {
throw new RuntimeException(sqlException);
}
}
我不知道出了什么问题以及为什么在我的控制台中返回:连接到数据库...在我的网页中返回的用户和密码无效!我认为问题出在我的 ConnectLogin 上,但是什么?