我想验证 从页面上得到的两个字段数据 和 从数据库中取出的两个字段数据 是否一致 怎么 才能 实现??
不用任何框架??index.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form method=get action="yanzheng.jsp">
<input type="text" name="username"><br>
<input type="text" name="password"><br>
<input type=submit value="提交">
</body>
</html>
yanzheng.jsp<%@ page language="java" import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body><%
String username=request.getParameter("username");
String password=request.getParameter("password");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password1="tiger";
Connection conn= DriverManager.getConnection(url,user,password1);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
// 我要在 这 验证 两个 字段的 数据 ?????? <%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 急用啊 !!!谢谢 啊!!
不用任何框架??index.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form method=get action="yanzheng.jsp">
<input type="text" name="username"><br>
<input type="text" name="password"><br>
<input type=submit value="提交">
</body>
</html>
yanzheng.jsp<%@ page language="java" import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body><%
String username=request.getParameter("username");
String password=request.getParameter("password");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password1="tiger";
Connection conn= DriverManager.getConnection(url,user,password1);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
// 我要在 这 验证 两个 字段的 数据 ?????? <%rs.close();
stmt.close();
conn.close();
%>
</body>
</html> 急用啊 !!!谢谢 啊!!
if(rs.next()) {
if(username.equals(rs.getString(1)) && password.equals(rs.getString(2)) )
<% { %>
用户名和密码正确 !
<%
}
else{
%
用户名或密码不正确!
<%} }%>
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
// 我要在 这 验证 两个 字段的 数据 ??????为什么要到循环外验证?难道还做一遍循环?
应该在循环内进行验证! if(username.equals(rs.getString(1)) && password.equals(rs.getString(2)) ){ %>
用户名和密码正确 !
<%} else { %
用户名或密码不正确!
<%}%>
String name;
String pwd;
while(rs.next())
{
name=rs.getString(1);
pwd=rs.getString(2);
}比较时 用这两个变量进行比较
if(username.equals(name) && password.equals(pwd) ){ %>
用户名和密码正确 !
<%} else { %
用户名或密码不正确!
<%}%>
呵呵,兄弟你误解了,代码不是在循环外执行的,也不是在循环内执行的,而是没有循环
只有if,用if代替while了,数据库校验字段只有一条记录,不需要 while,