验证学号姓名是否与数据库中一致,很奇怪为什么一直跳转到验证错误的界面,有javabean
下面是代码
第一部分:
package beans;
public class C {
private String id;
private String name;
public C(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二部分
package beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import beans.C;
public class CDao {
Connection con=null;
PreparedStatement sql=null;
ResultSet rs=null;
public Connection getConnenction(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException event){}
try {
//建立连接
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl1", "omama", "123");
}catch (SQLException e){
e.printStackTrace();
}
return con;
}
public boolean verify(C c) throws SQLException{
System.out.println(c);
con=getConnenction();
boolean flag=false;
try{
sql=con.prepareStatement("select * from c where id = ? and name = ?");
sql.setString(1, c.getId());
sql.setString(2, c.getName());
rs=sql.executeQuery();
System.out.println(rs.next());
System.out.println(rs);
if(rs.next()){
System.out.println("sd");
flag=true;
}
}
catch (SQLException e){
e.printStackTrace();
}
finally
{
if(rs!= null) rs.close();
if(sql!= null) sql.close();
if(con != null) con.close();
}
return flag;
}
}
第三部分
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="c" class="beans.C" scope="session"></jsp:useBean>
<jsp:useBean id="cDao" class="beans.CDao" scope="session"></jsp:useBean>
<%
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>管理员登录页面</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">
-->
<script type="text/javascript">
function regist(){
window.open("register.jsp");
//location.href = "register.jsp";
}
</script>
</head>
<body bgcolor="pink">
<%
request.setCharacterEncoding("utf-8");
String errinfo = (String)request.getAttribute("errinfo");
if(errinfo==null){errinfo="";}
%>
<h2 align="center">温州大学学生用户管理系统</h2>
<hr color="blue">
<form action="A/checklogin.jsp" method="post">
<table align="center" border="1" bgcolor="#0099CC">
<th colspan="2">管理员登录</th>
<tr>
<td> 输入学号:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td> 输入姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="提交"> <input type="button" value="注册" onclick="regist()"></td>
</tr>
<tr>
<td align="center" colspan="2"><font color="red"><%=errinfo %></font></td>
</tr>
</table>
</form>
</body>
</html>
第四部分
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="c" class="beans.C" scope="session"></jsp:useBean>
<jsp:useBean id="cDao" class="beans.CDao" scope="session"></jsp:useBean>
<%
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 'checklogin.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>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:setProperty property="*" name="c" />
<%=c.getId()%>
<%=c.getName()%>
<%
if(cDao.verify(c)){
//response.sendRedirect("mainframe.jsp");
request.getRequestDispatcher("mainframe.jsp").forward(request, response);
//rs.next()
}else{
System.out.println("wwcdddd");
response.sendRedirect("c.jsp");
}
%>
<br>
</body>
</html>
下面是代码
第一部分:
package beans;
public class C {
private String id;
private String name;
public C(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二部分
package beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import beans.C;
public class CDao {
Connection con=null;
PreparedStatement sql=null;
ResultSet rs=null;
public Connection getConnenction(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException event){}
try {
//建立连接
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl1", "omama", "123");
}catch (SQLException e){
e.printStackTrace();
}
return con;
}
public boolean verify(C c) throws SQLException{
System.out.println(c);
con=getConnenction();
boolean flag=false;
try{
sql=con.prepareStatement("select * from c where id = ? and name = ?");
sql.setString(1, c.getId());
sql.setString(2, c.getName());
rs=sql.executeQuery();
System.out.println(rs.next());
System.out.println(rs);
if(rs.next()){
System.out.println("sd");
flag=true;
}
}
catch (SQLException e){
e.printStackTrace();
}
finally
{
if(rs!= null) rs.close();
if(sql!= null) sql.close();
if(con != null) con.close();
}
return flag;
}
}
第三部分
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="c" class="beans.C" scope="session"></jsp:useBean>
<jsp:useBean id="cDao" class="beans.CDao" scope="session"></jsp:useBean>
<%
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>管理员登录页面</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">
-->
<script type="text/javascript">
function regist(){
window.open("register.jsp");
//location.href = "register.jsp";
}
</script>
</head>
<body bgcolor="pink">
<%
request.setCharacterEncoding("utf-8");
String errinfo = (String)request.getAttribute("errinfo");
if(errinfo==null){errinfo="";}
%>
<h2 align="center">温州大学学生用户管理系统</h2>
<hr color="blue">
<form action="A/checklogin.jsp" method="post">
<table align="center" border="1" bgcolor="#0099CC">
<th colspan="2">管理员登录</th>
<tr>
<td> 输入学号:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td> 输入姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="提交"> <input type="button" value="注册" onclick="regist()"></td>
</tr>
<tr>
<td align="center" colspan="2"><font color="red"><%=errinfo %></font></td>
</tr>
</table>
</form>
</body>
</html>
第四部分
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="c" class="beans.C" scope="session"></jsp:useBean>
<jsp:useBean id="cDao" class="beans.CDao" scope="session"></jsp:useBean>
<%
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 'checklogin.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>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:setProperty property="*" name="c" />
<%=c.getId()%>
<%=c.getName()%>
<%
if(cDao.verify(c)){
//response.sendRedirect("mainframe.jsp");
request.getRequestDispatcher("mainframe.jsp").forward(request, response);
//rs.next()
}else{
System.out.println("wwcdddd");
response.sendRedirect("c.jsp");
}
%>
<br>
</body>
</html>