|
1 login.jsp
[php]
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>jsp连接数据库测试</title>
<script language="javascript">
function login(frm)
{
if(frm.userName.value == "")
{
alert("用户名不能为空");
frm.userName.focus();
return false;
}
else if(frm.userPassword.value == "")
{
alert("密码不能为空");
frm.userPassword.focus();
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<center>
<form name="frm" method="post" action="login_confirm.jsp" onSubmit="return login(this)">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="userPassword"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="提交"></td>
</tr>
</table>
</form>
现在是北京时间:
<%= new java.util.Date().toLocaleString()%>
</center>
</body>
</html>
[/php]
2 login_confirm.jsp
[php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*"%>
<jsp:useBean id="query" class="testBean.QueryBean" scope="page"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>连接数据库</title>
</head>
<body>
<%
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
out.println(userName);
out.println(userPassword);
int flag = query.demand(userName,userPassword);
out.println(flag);
/*switch(flag)
{
case 1:
response.sendRedirect("succeed.html");
break;
case 0:
response.sendRedirect("fail.html");
break;
}*/
%>
</body>
</html>
[/php]
3 testBean
[php]
package testBean;
import java.sql.*;
import javax.sql.*;
import java.io.*;
public class QueryBean
{
private PreparedStatement pstmt = null;
private Connection con = null;
private ResultSet rs = null;
public int demand(String userName,String userPassword)
{
System.out.println(userName);
System.out.println(userPassword);
int flag = 0;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jxd","root","");
pstmt = con.prepareStatement("SELECT username,userpassword FROM user_test WHERE username=? AND userpassword=?");
pstmt.setString(1,userName);
pstmt.setString(2,userPassword);
rs = pstmt.executeQuery();
if(rs.next())
{
flag = 1;
}
rs.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return flag;
}
}
[/php]
问题是无论输入的数据正确与否,都只输出0!!为什么?
jdk:1.5.0
tomcat:5.0.25
数据库:jxd
表:user_test
谢谢大家! |
|