- 爱易网页
-
Java教程
- JSP连接SQL,代码没有出现异常,但连接返回空指令,实在找不出原因了!
日期:2014-05-18 浏览次数:20738 次
JSP连接SQL,代码没有出现错误,但连接返回空指令,实在找不出原因了!!
这里是java代码:
public boolean checkAdmin(String username, String password)//检查管理员的登录名和密码是否正确
throws UserException {
//创建一个变量用于判断结果
boolean isAdmin = false;
//查询admin表的SQL语句
String selectSql = "SELECT * FROM admin WHERE username=? AND password=? ";
PreparedStatement pmst = null;
try{
//创建一个数据库连接对象
ConnectDB conn = new ConnectDB();
//执行查询语句
pmst = conn.getConnection().prepareStatement(selectSql);
//1代表查询语句中的?号,username为形参
pmst.setString(1, username);
//1代表查询语句中的?号,username为形参
pmst.setString(2, password);
// pmst.executeQuery();
ResultSet rst = pmst.executeQuery();
//判断结果是否为空,如否,说明用户名和密码是正确的
if(rst.next()){
isAdmin = true;
}
这里JSP的检测页面的代码:
<%@ page language= "java " import= "java.util.*,userJSP.* " pageEncoding= "gbk "%>
<%
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 'cklogin.jsp ' starting page </title>
<link rel= "stylesheet " href= "images/css.css " type= "text/css " media= "screen ">
<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>
<%
String userName = request.getParameter( "username ");
String passWord = request.getParameter( "password ");
if(userName==null || (userName = userName.trim()).length() == 0) {
session.setAttribute( "error ", "登录失败,请输入您的登录名。 ");
response.sendRedirect( "error.jsp ");
}
if(passWord==null || passWord.length() == 0) {
session.setAttribute( "error ", "登录失败,请输入您的密码。 ");
response.sendRedirect( "error.jsp ");
}
UserInter objDB = UserInfoFactory.getUserInfoDB();