1
回答
jsp+javabean+mysql简单登录系统的验证问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我用纯jsp可以通过jdbc连接数据库,在页面显示数据库的信息,但是用jsp+javabean就只能测试连接到数据库,并没有做相应的验证。

user包

package com.model.user;

public class User {
		
				private String username;
				private String password;
				public String getUsername() {
					return username;
				}
				public void setUsername(String username) {
					this.username = username;
				}
				public String getPassword() {
					return password;
				}
				public void setPassword(String password) {
					this.password = password;
				}
				
}

数据库

package com.model.DAO;

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

//数据库的连接
public class DB {

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;

	// 执行数据库查询并返回查询结果
	public  ResultSet query(String sql) {
		try {
			// 数据库帐号密码
			String URL = "jdbc:mysql://127.0.0.1:3306/js";
			String USER = "root";
			String PASSWORD = "12345";
			// 1.加载数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 2.连接数据库
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println("数据库验证");
		
		} 
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return rs;
	}

	// 关闭数据库连接
	public void close() {
		try {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}



登录界面
<html>
<body>
  
  <center>
  <h1>用户登录</h1><hr>
  <form action="dologin.jsp" name="form1" method="post">
  用户名:<input type="text" name="username"><br>
  密   码:<input type="password" name="password"><br>
  <input type="submit" value="提交" name="submit"/>
  <input  type="reset" value="重置" name="reset"/> 
</form>
  </center>
  
  </body>
</html>



dologin.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
	String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <jsp:useBean id="user" class="com.model.user.User" scope="session"></jsp:useBean>
  <jsp:useBean id="DAO" class="com.model.DAO.DB"></jsp:useBean>
  <jsp:setProperty property="*" name="user"/>
  <body>
  <%
  							//获取帐号、密码
  						String u=user.getUsername();
  						String  pwd=user.getPassword();
  					    String sql = "select password from user where username= ' "+u+ "  ' ";
  						 ResultSet rs=DAO.query(sql);
  						
  						 if(rs.next())
  						 {
  						 		if(rs.getString(1).equals(pwd))
  						 				System.out.println("登录成功");
  						 		else
  						 		{
  						 				System.out.println("登录失败");
  						 		}
  						 }
  						 
  						
   %>
  
  </body>
</html>





为什么只能到数据库验证,不能进行相应的帐号密码判断?数据库的帐号密码是admin



举报
打不死
发帖于2年前 1回/562阅
顶部