package cn.hlxk.com.userDaoImp;import java.sql.*;import cn.hlxk.com.user.DB.DBConn;
import cn.hlxk.com.user.vo.User;
import cn.hlxk.com.userDao.UserDao;public class UserDaoImp implements UserDao {
private static final String isLoginSQL="select count(*) from customer where name=?"; public Boolean isLogin(User user) {
// TODO Auto-generated method stub
Boolean isLogin=false;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=new DBConn().getConnection();
ps=conn.prepareStatement(this.isLoginSQL);
ps.setString(1, user.getName());
rs=ps.executeQuery();
if(rs.next()){
int i=rs.getInt(1);
System.out.println(i);
if(i==0){
isLogin=true;
}
}
}catch (Exception e) {
// TODO: handle exception
//throw new RuntimeException(e.getMessage(),e);
}finally{
close(conn, ps, rs);
}
return isLogin;
}
private void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
rs.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
try {
ps.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
try {
conn.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
import cn.hlxk.com.user.vo.User;
import cn.hlxk.com.userDao.UserDao;public class UserDaoImp implements UserDao {
private static final String isLoginSQL="select count(*) from customer where name=?"; public Boolean isLogin(User user) {
// TODO Auto-generated method stub
Boolean isLogin=false;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=new DBConn().getConnection();
ps=conn.prepareStatement(this.isLoginSQL);
ps.setString(1, user.getName());
rs=ps.executeQuery();
if(rs.next()){
int i=rs.getInt(1);
System.out.println(i);
if(i==0){
isLogin=true;
}
}
}catch (Exception e) {
// TODO: handle exception
//throw new RuntimeException(e.getMessage(),e);
}finally{
close(conn, ps, rs);
}
return isLogin;
}
private void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
rs.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
try {
ps.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
try {
conn.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
就是我去获取数据库里的数据字段 name,数据库也有数据,可为何获取到的结果都为o
int i=rs.getInt(1);
System.out.println(i);
if(i==0){
isLogin=true;
}我看呀,吧if(rs.next())改成while(rs.next())应该能解决
int i=rs.getInt(1);
System.out.println(i);
if(i==0){
isLogin=true;
}我看呀,吧if(rs.next())改成while(rs.next())应该能解决这个应该可以吧
或者 打印 user.getName() 的值看看
int的0不是Integer的 null收工
import java.io.PrintWriter;import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import cn.hlxk.com.user.vo.User;
import cn.hlxk.com.userDaoImp.UserDaoImp;public class MyServlet extends HttpServlet implements Servlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码以及接收参数
request.setCharacterEncoding("GBK");
String name=request.getParameter("name");
String pass=request.getParameter("pass");
User us=new User();
us.setName("name");
us.setPass("pass");
boolean login=new UserDaoImp().isLogin(us);
System.out.println(login);
if(new UserDaoImp().isLogin(us)){
response.sendRedirect("loginSuc.html");
}else {
response.sendRedirect("loginFail.html");
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
应该是这个有问题
改成
select * from customer where name=? and password=?
你那都没判断密码肯定是所有账号都能登录的
if(rs.next()){
int i=rs.getInt(1);
System.out.println(i);
if(i==0){
isLogin=true;
}
这里可以改成if(rs.getRows()!= 0)
{
return true;
}
前提是你要确保name字段里没有重复的
不是把所有的数据都查询出来了吗?那样比较浪费服务器的资源哦