菜鸟求助 简单的登陆验证 jsp+javabean+mysql

lansebaobei222 2010-01-13 08:00:27
输入数据库中的数据不能登陆,大家大姐们帮帮忙 给解释解释 小弟刚开始学 很多地方不懂
代码:
html:
<html>
<head>
<title>管理员登陆</title>
</head>
<body>
<form action="yanzheng.jsp" ,method="post">
用户名:<input type="text" name="name">
密码:<input type="password" name="password">
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
jsp:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="ff.admincheck"%>
<%@ page import="ff.adminhe"%>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
adminhe user=new adminhe();
user.setname(name);
user.setpassword(password);
admincheck check=new admincheck(user);

if(check.checka())
{
out.println("登陆成功");
response.sendRedirect(response.encodeUrl("http://localhost:8080/news1/addnews.jsp"));
}
else
{
out.println("登陆失败");
}
%>
javabean:
package ff;

public class adminhe {
String name;
String password;
public void setname(String name)
{
this.name=name;
}
public String getname()
{
return name;
}
public void setpassword(String password)
{
this.password=password;
}
public String getpassword()
{
return password;
}
}



package ff;

import ff.adminhe;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class admincheck {
protected adminhe user;
public admincheck()
{

}
public admincheck(adminhe user)
{
this.user=user;
}
public adminhe getuser()
{
return user;
}
public boolean checka() throws ClassNotFoundException, SQLException,InstantiationException,IllegalAccessException
{

Class.forName("com.mysql.jdbc.Driver" ).newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/admin?user=root&password=root");
Statement stmt=conn.createStatement();
String sql="SELECT*FROM adminin";
ResultSet rs=stmt.executeQuery(sql);
rs.next();
{

String dd=rs.getString(1);
String gg=rs.getString(2);
String name= user.getname();
String password=user.getpassword();
rs.close();
stmt.close();
conn.close();

if((name.equals(dd))&&(password.equals(gg)))
{
return true;
}
else
{
return false;
}
}

}
}
我觉着问题应该是红色部分 但不知道对不对 也不知道如何改?哥哥姐姐们帮帮忙啊 给解释下
...全文
546 49 打赏 收藏 转发到动态 举报
写回复
用AI写文章
49 条回复
切换为时间正序
请发表友善的回复…
发表回复
ineedaname 2010-01-16
  • 打赏
  • 举报
回复
我机器上没MYsql所以没办法找那个错 ,。。不好意思 ,~
lansebaobei222 2010-01-14
  • 打赏
  • 举报
回复
开始的问题是粗心造成的连成了另一个数据库的一表造成错误 无法登陆
后边一问题 在while循环中加入break语句实现了想要实现的功能具体代码如下:
public boolean checka() throws ClassNotFoundException, SQLException,InstantiationException,IllegalAccessException
{

Class.forName("com.mysql.jdbc.Driver" ).newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/news?user=root&password=root");
Statement stmt=conn.createStatement();
String sql="SELECT*FROM admin";
ResultSet rs=stmt.executeQuery(sql);
int i=0;

while(rs.next())
{
String f3=rs.getString(1);
String f4=rs.getString(2);
String name= user.getName();
String password=user.getPassword();

if((name.equals(f3))&&(password.equals(f4)))
{
i=1;
}
if(i==1) break;
}


rs.close();
stmt.close();
conn.close();
if(i==1)
{
return true;
}
else
{
return false;
}

}

}
没想到有这么多好心的哥哥姐姐们的帮助 所以开始分定的有点少 不好意思 谢谢大家的帮助啊 十分感谢
zisehaizi 2010-01-14
  • 打赏
  • 举报
回复
看看
lansebaobei222 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 42 楼 ineedaname 的回复:]
按照楼主的要求,我重写了bean和dao。不过我的机器上没sql所以没测试,呵呵。
另外建议获取连接和关闭对象用工厂,看起来清爽很多
楼主注意命名的规范啊,类名开头字母大写,命名最好从名字上能看出意思的,另外get和set方法你是自己写的吗?可以直接生成的 alt+shift+s 选择Generate Getters and setters
javaBeanJava codepublicclass Adminhe {private String name;private String password;public String getName() {return name;
}publicvoid setName(String name) {this.name= name;
}public String getPassword() {return password;
}publicvoid setPassword(String password) {this.password= password;
}

}
DaoJava codeimport java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;publicclass Admincheck {private Connection conn=null;private Statement stmt=null;private ResultSet rs=null;/**
*@param adminhe
* Adminhe对象
*@return true:此用户存在 false:无此用户
*@throws ClassNotFoundException
*@throws SQLException
*@throws InstantiationException
*@throws IllegalAccessException*/publicboolean checkNameAndPassword(Adminhe adminhe)throws ClassNotFoundException, SQLException,
InstantiationException, IllegalAccessException {

System.out.println("待验证用户名:"+ adminhe.getName()+"密码:"+ adminhe.getPassword());
System.out
.println("======================我是分隔符===========================");boolean flag=false;// 注册驱动 Class.forName("com.mysql.jdbc.Driver").newInstance();// 获取数据库连接 conn= DriverManager
.getConnection("jdbc:mysql://localhost:3306/admin?user=root&password=root");// 获得Statement对象 stmt= conn.createStatement();
String sql="SELECT*FROM adminin";// 执行sql获得结果集 rs= stmt.executeQuery(sql);while (rs.next()) {
System.out.println("数据库name:"+ rs.getString(1));
System.out.println("数据库password:"+ rs.getString(2));// 查找的记录中当此行的第一列String与name相同且第二列String与password相同时,表示此用户在数据库中存在if (rs.getString(1).equals(adminhe.getName())&& rs.getString(2).equals(adminhe.getPassword())) {
flag=true;
}
}
rs.close();
stmt.close();
conn.close();
System.out.println(flag==true?"验证pass" :"无此用户");return flag;
}
}

[/Quote]
恩 编码规范很多 还得多向你学习 谢谢啊 你的代码运行的时候有点错误 我太菜了 没找到原因 不过十分感谢帮助和建议 我也觉得我写的代码太乱 缺乏条例 有点想到那写到哪 谢谢啊 分开始定的少了不好意思
lansebaobei222 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 tzsword 的回复:]
Java codepublicclass MyDAO{/**
* 查询所有
*@return
*@throws SQLException*/public List findUser(String uName)throws SQLException
{

db=DbConnect.getInstance();
conn=db.getConnect();

sql="select * from User where uName="+uName;
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
list=new ArrayList();while(rs.next())
{
User user=new User();
user.setUname(rs.getString("uname"));
user.setPassword(rs.getString("password"));
list.add(user);
}
rs.close();
ps.close();
conn.close();return list;
}
}


Java codepublicclass MyJavaBean{public String checkUser(String uName,Strng pwd){
List<User> uList=MyDAO.findUser(uName);if(uList.size()<=0){return"用户名输入不正确"};for(int i=0;i<uList.size();i++){if(uList.get(i).getPassword.equals(pwd)){return"该用户名可用";
}else{return"密码错误";
}
}
}
}
[/Quote]

问题终于解决了 谢谢了啊 分定的有点少了 不好意思啊 十分感谢
lansebaobei222 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 liuxiaohong111 的回复:]
哈哈
[/Quote]
谢谢 啊 不过我用原来的方法改进了下 能正常实现了 改的在最后 没想到有这么多的高手帮忙,分有点少了 不好意思啊 谢谢
xu101q 2010-01-14
  • 打赏
  • 举报
回复
检查你的的数据库表中的字段值是不是有空格之类的~~~~~~~~
打印一个出来看看。。。
如果有空格是不能正确判断的 !
yzsunlight 2010-01-14
  • 打赏
  • 举报
回复
检查Mysql的编码格式是否正确
  • 打赏
  • 举报
回复
Password 和name的值貌似没有传进去
ineedaname 2010-01-13
  • 打赏
  • 举报
回复
按照楼主的要求,我重写了bean和dao。不过我的机器上没sql所以没测试,呵呵。
另外建议获取连接和关闭对象用工厂,看起来清爽很多
楼主注意命名的规范啊,类名开头字母大写,命名最好从名字上能看出意思的,另外get和set方法你是自己写的吗?可以直接生成的 alt+shift+s 选择Generate Getters and setters
javaBean
public class Adminhe {
private String name;
private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

Dao
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Admincheck {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;

/**
* @param adminhe
* Adminhe对象
* @return true:此用户存在 false:无此用户
* @throws ClassNotFoundException
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public boolean checkNameAndPassword(Adminhe adminhe)
throws ClassNotFoundException, SQLException,
InstantiationException, IllegalAccessException {

System.out.println("待验证用户名:" + adminhe.getName() + "密码:"
+ adminhe.getPassword());
System.out
.println("======================我是分隔符===========================");
boolean flag = false;
// 注册驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 获取数据库连接
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/admin?user=root&password=root");
// 获得Statement对象
stmt = conn.createStatement();
String sql = "SELECT*FROM adminin";
// 执行sql获得结果集
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("数据库name:" + rs.getString(1));
System.out.println("数据库password:" + rs.getString(2));
// 查找的记录中当此行的第一列String与name相同且第二列String与password相同时,表示此用户在数据库中存在
if (rs.getString(1).equals(adminhe.getName())
&& rs.getString(2).equals(adminhe.getPassword())) {
flag = true;
}
}
rs.close();
stmt.close();
conn.close();
System.out.println(flag == true ? "验证pass" : "无此用户");
return flag;
}
}

liuxiaohong111 2010-01-13
  • 打赏
  • 举报
回复
哈哈
TzSword 2010-01-13
  • 打赏
  • 举报
回复

public class MyDAO{
/**
* 查询所有
* @return
* @throws SQLException
*/
public List findUser(String uName) throws SQLException
{

db=DbConnect.getInstance();
conn=db.getConnect();

sql="select * from User where uName="+uName;
ps =conn.prepareStatement(sql);
rs=ps.executeQuery();
list =new ArrayList();
while(rs.next())
{
User user=new User();
user.setUname(rs.getString("uname"));
user.setPassword(rs.getString("password"));
list.add(user);
}
rs.close();
ps.close();
conn.close();
return list;
}
}




public class MyJavaBean{
public String checkUser(String uName,Strng pwd){
List<User> uList=MyDAO.findUser(uName);
if(uList.size()<=0){return "用户名输入不正确"};

for(int i=0;i<uList.size();i++){
if(uList.get(i).getPassword.equals(pwd)){
return "该用户名可用";
} else{
return "密码错误";
}
}
}
}
lansebaobei222 2010-01-13
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 tzsword 的回复:]
我想起来了,你知道DAO吗?
[/Quote]
十分感谢 今晚上 打扰了啊 谢谢
lansebaobei222 2010-01-13
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 liuxiaohong111 的回复:]
哪有这样的查的啊 通过用户名,密码 查询数据库,查询标志管理员的那个字段,如果有记录,就判断,是管理员,做什么,不是管理员做什么,如果没有返回记录,则没有这个用户
[/Quote]
额 对 这样就不用循环了 直接提取比较就行了 刚开始接触这些东西 不大懂 谢谢啊
liuxiaohong111 2010-01-13
  • 打赏
  • 举报
回复
哪有这样的查的啊 通过用户名,密码 查询数据库,查询标志管理员的那个字段,如果有记录,就判断,是管理员,做什么,不是管理员做什么,如果没有返回记录,则没有这个用户
lansebaobei222 2010-01-13
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 tzsword 的回复:]
我想起来了,你知道DAO吗?
[/Quote]
JAVA 关于Dao
悬赏分:50 - 解决时间:2009-8-24 10:53
package com.haha.dao;

import *****;

import com.huiwen.bean.Dept;
import com.haha.bean.Student;
import com.haha.db.DbConnect;

public class Dao {
private DbConnect db=null;
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
private List list;
private Dept dept;
String sql="";
//和分页相关的变量
private int perPageSize;
private int rowNum;
private int showPage;
private int currentPage;
private int pageNum;
/**
* 登陆的方法
* @param st
* @return
* @throws SQLException
*/
public boolean login(Student st) throws SQLException
{
db=DbConnect.getInstance();
conn=db.getConnect();
boolean login =false;
sql="select count(*) from student where username =? and password =?";
ps =conn.prepareStatement(sql);
ps.setString(1, st.getUsername());
ps.setString(2, st.getPassword());
rs=ps.executeQuery();
while(rs.next())
{
if(rs.getInt("count(*)")==1)
{
login=true;
}
}
rs.close();
ps.close();
conn.close();
return login;

}
/**
* 查询所有
* @return
* @throws SQLException
*/
public List getDpets() throws SQLException
{

db=DbConnect.getInstance();
conn=db.getConnect();

sql="select * from dept";
ps =conn.prepareStatement(sql);
rs=ps.executeQuery();
list =new ArrayList();
while(rs.next())
{ dept =new Dept();
dept.setDeptid(rs.getInt("deptid"));
dept.setDeptname(rs.getString("deptname"));
dept.setDeptno(rs.getString("deptno"));
dept.setLeader(rs.getString("leader"));
list.add(dept);
}
rs.close();
ps.close();
conn.close();
return list;

}
//删除
public void del(Dept dept2) throws SQLException
{
db=DbConnect.getInstance();
conn=db.getConnect();

sql="delete from dept where deptid=?";
ps =conn.prepareStatement(sql);
ps.setInt(1, dept2.getDeptid());
ps.executeUpdate();



ps.close();
conn.close();


}
public List query(Dept dept) throws SQLException {
db=DbConnect.getInstance();
conn=db.getConnect();

sql="select * from dept where 1=1 ";
if(dept.getDeptname()!=null||!"".equals(dept.getDeptname()))
{
sql=sql+"and deptname like '%"+dept.getDeptname()+"%'";

}
if(dept.getLeader()!=null||!"".equals(dept.getLeader()))
{
sql=sql+"and leader like '%"+dept.getLeader()+"%'";

}
ps=conn.prepareStatement(sql);
rs =ps.executeQuery();
list =new ArrayList();
while(rs.next())
{
dept =new Dept();
dept.setDeptid(rs.getInt("deptid"));
dept.setDeptname(rs.getString("deptname"));
dept.setDeptno(rs.getString("deptno"));
dept.setLeader(rs.getString("leader"));
list.add(dept);
}
rs.close();
ps.close();
conn.close();
return list;
}
麻烦详解每个步骤
这个?
lansebaobei222 2010-01-13
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 tzsword 的回复:]
我想起来了,你知道DAO吗?
[/Quote]
不知道 DAO? 查查啊
lansebaobei222 2010-01-13
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 liuxiaohong111 的回复:]
while((!rs.next())&&(i==0));  这是干嘛用的啊
[/Quote]
就是和数据库里的记录比较 如果是管理员循环结束或数据库里没记录结束
liuxiaohong111 2010-01-13
  • 打赏
  • 举报
回复
while((!rs.next())&&(i==0)); 这是干嘛用的啊
TzSword 2010-01-13
  • 打赏
  • 举报
回复
我想起来了,你知道DAO吗?
加载更多回复(29)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧