我的servlet中的查询数据库语句有问题!

gwang820124 2004-09-23 08:13:00
try{
Statement stm=conn.createStatement();
ResultSet result=stm.executeQuery(sql);
String tempPassword=new String();
String userId=new String();
while(result.next()){ ————————————————*******
m++;tempPassword=result.getString("password");
userId=result.getString("userId");
}
//于用户填入的密码作比较
if(tempPassword.matches(request.getParameter("password"))){
session.setAttribute("userName",request.getParameter("user"));
session.setAttribute("userId",userId);m++;
gotoPage("/Exec/index1.jsp",request,response);
}else{
response.sendRedirect("../errpage.jsp");
}

}catch(Exception e){
System.err.println(e);
e.printStackTrace(System.err);
}

在eclipse下编译产生异常:
java.lang.NullPointerException

经过检查应该是*那一句,有问题,请指点!
谢谢!
在线等!
...全文
303 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
我已经找到答案了!谢谢!
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
你的意思是不啊这些事情放在一个JSP文件里面处理是吗?
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
但是我们想在登录了用户名和密码后,想用servlet来处理!
这样做可以吗?
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
package db;

import java.util.*;
import java.sql.*;
import java.io.*;

public class DbConnection{
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

public DbConnection(){
}

/************************************************
* openConnection
* read file "db.txt"
************************************************/
public boolean openConnection(){
Properties prop = new Properties();
try{
InputStream is = getClass().getResourceAsStream("db.txt");
prop.load(is);
if(is != null) is.close();
}catch(IOException e){
System.out.println("[DbConnection] Open db.txt File, Error!");
}

String jdbc = prop.getProperty("driver");
String uri = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");

//System.out.println("jdbc=[" + jdbc + "]");
//System.out.println("uri=[" + uri + "]");

try{
Class.forName(jdbc).newInstance();
}catch(ClassNotFoundException e){
System.out.println("JDBC login, Error!@" + e.getMessage());
return false;
}catch (Exception e){
System.err.println("Unable to load driver!");
e.printStackTrace();
}

try{
this.conn = DriverManager.getConnection (uri,user,password);
}catch(SQLException e){
System.out.println("Generate Connection, Error!" + e.getMessage());
return false;
}
return true;

}


/*************************************************
* executeQuery and executeUpdate
* query and update DB
*************************************************/
public ResultSet executeQuery(String query) throws SQLException{
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
return rset;
}

public void executeUpdate(String query) throws SQLException{
stmt = conn.createStatement();
stmt.executeUpdate(query);
if(stmt != null) stmt.close();
}

public void close() throws SQLException{
if(conn != null) conn.close();
if(rset != null) rset.close();
if(stmt != null) stmt.close();
}

protected void finalize() throws Throwable{
close();
}

public static void main(String[] args){
//Design for test purpose.
DbConnection dc = new DbConnection();
System.out.println(dc.openConnection());
}

}
这个是我的JAVABEAN。没有带连接池的。
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
首先在登陆页面填写了密码和用户名,
然后提交给一个JSP页面处理
然后在JSP页面里面调用JAVABEAN处理数据库方面的事。
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
你的bean呢?
什么内容?
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
我是这样写的
<%@ page language="java" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="LoginBean" scope="page" class="db.DbConnection" />
<%
String pub=request.getParameter("pub");
String uname=request.getParameter("uname");
String passwd=request.getParameter("passwd");

if (pub.length()>0) {
LoginBean.openConnection();
String sql = "select count(*) as num_user from user where name=\""+ uname +"\" and passwd=\""+ passwd +"\"";
//String sql="select ID,name,nick,passwd,post from user where name=\""+ uname +"\" and passwd=\""+ passwd +"\"";
ResultSet rs = LoginBean.executeQuery(sql);
int num_user = 0;
int U_ID=0;
//rs.last();
//int num_user=rs.getRow();
if (rs.next()) {
num_user = rs.getInt("num_user");
}
rs.close();
if (num_user==1) {

sql="select ID,name,nick,passwd,post from user where name=\""+ uname +"\" and passwd=\""+ passwd +"\"";
//rs.first();
rs = LoginBean.executeQuery(sql);
if (rs.next()) {



U_ID=rs.getInt("ID");
String U_nick=rs.getString("nick");
int U_post=rs.getInt("post");
session.putValue("ID",String.valueOf(U_ID));
session.putValue("name",uname);
session.putValue("nick",U_nick);
session.putValue("passwd",passwd);
session.putValue("post",String.valueOf(U_post));
}
rs.close();



sql="select logintime,cishu from user where ID="+ U_ID;
rs = LoginBean.executeQuery(sql);

String logintime = null;
if (rs.next()) {
logintime=rs.getString("logintime");
String cishu=rs.getString("cishu");
}
rs.close();
Cookie Thelogintime=new Cookie("cookielogintime",logintime);
response.addCookie(Thelogintime);


sql="select (UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(likai)) as nstaytime from user where ID="+ U_ID;
rs = LoginBean.executeQuery(sql);

int nstaytime=0;
if (rs.next()) {

nstaytime=rs.getInt("nstaytime");
nstaytime=nstaytime/3600;

}
rs.close();

sql="select power,hpower from userdata where UID="+ U_ID;
rs = LoginBean.executeQuery(sql);
int power=0;
int hpower=0;
int hfpower=0;
if (rs.next()) {

power=rs.getInt("power");
hpower=rs.getInt("hpower");

}
rs.close();

hfpower=hpower*nstaytime/12;
int zpower=hfpower+power;

if (zpower>=hpower) {
hfpower=hpower-power;
}

sql="update userdata set nstaytime="+ nstaytime +",power=power+"+ hfpower +" where UID="+ U_ID;
LoginBean.executeUpdate(sql);

sql="update user set logintime=now(),cishu=cishu+1 where ID="+ U_ID;
LoginBean.executeUpdate(sql);
LoginBean.close();
response.sendRedirect("main.jsp");

}
else {

out.println("用户名或者密码错误!<a href='javascript:history.go(-1);'>返回</a>");

}
}
else {

out.println("URL连接错误");

}
%>
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
对呀!我是在实现登陆,但是我没有用javabean!
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
恩 我想你是想处理上级网页代码的的请求来判断
用户是否能够登陆,对吗?
hanwei39 2004-09-23
  • 打赏
  • 举报
回复
学习
hanwei39 2004-09-23
  • 打赏
  • 举报
回复
学习
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
你是不是在JAVABEAN里面处理上级网页代码的请求的
如果是这样的话,你上级网页代码的请求JAVABEAN是不会得到的
所以你的user当然是空了
我自己也去试过了
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
自己顶!
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
在线等待!
谢谢了!
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
好的 我看看后,试一下洛。
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="../header.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<style type="text/css">
<!--
a {text-decoration:none}
a:hover {color: red;text-decoration:none}

.style1 {
color: #FFFFFF;
font-size: 14px;
font-family: "黑体";
font-weight: bold;
}
.style2 {
color:#FF0000;
font-size:14px;
font-family:"黑体";
font-weight:bold;
}
.style3 {
color:#FF0000;
font-size:16px;
font-family:"隶书";
font-weight:bold;
}
.style4 {
color: #FFFFFF;
font-size: 16px;
font-family: "黑体";
font-weight: bold;
}
.style5 {
color: #666666;
font-size: 14px;
font-family: "宋体";

}
.style6 {
color: #000000;
font-size: 10px;
font-family: Verdana, Arial, Helvetica, sans-serif
}
-->
</style>
</head>

<body>
<table width="1000" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="150" height="551"> </td>
<td width="804" valign="top">
<table width="780" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="160" rowspan="3" valign="top" bgcolor="#CCCCCC">
<table width="160" border="0" cellpadding="0" cellspacing="0" bgcolor="#FF0000">
<!--DWLayoutTable-->
<tr>
<td width="5%" height="20" align="right" valign="middle"><img src="./picture/image_25.gif"> </td>
<td width="15%" align="left" valign="middle"><a href="../Exec/aboutus/about1.jsp"><span class="style1">关于PEGASUS</span></a></td>
</tr>
<tr>
<td width="5%" height="20" align="right" valign="middle"><img src="./picture/image_25.gif"> </td>
<td width="15%" align="left" valign="middle"><a href="../Exec/news.jsp"><span class="style1">新闻动态</span></a></td>
</tr>
<tr>
<td width="5%" height="20" align="right" valign="middle"><img src="./picture/image_25.gif"> </td>
<td width="15%" align="left" valign="middle"><span class="style1">产品简介</span></td>
</tr>
<tr>
<td width="5%" height="20" align="right" valign="middle"><img src="./picture/image_25.gif"> </td>
<td width="15%" align="left" valign="middle"><span class="style1">营销网络</span></td>
</tr>
<tr>
<td width="5%" height="20" align="right" valign="middle"><img src="./picture/image_25.gif"> </td>
<td width="15%" align="left" valign="middle"><span class="style1">联系我们</span></td>

</tr>
</table>
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">用 户</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="text" name="user" size="14"></td>
</tr>
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">密 码</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="password" name="password" size="14"></td>
</tr>
<tr> </tr>
</table> <table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" height="30" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="regist.jsp"><img src="./picture/zhuce_off.gif" border="0"></a></td>
<td width="10%" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="/Exec/servlet/login.Login_server"><img src="./picture/denglu_off.gif" border="0"></a></td>
</tr>
</table>
</td>
<td width="11" height="17"> </td>
<td width="462"> </td>
<td width="20"> </td>
<td width="131"> </td>
</tr>
<tr>
<td height="27"> </td>
<td colspan="2" align="left" valign="bottom"><span class="style3">  新闻动态</span></td>
<td> </td>
</tr>
<tr>
<td height="202"> </td>
<td rowspan="2" valign="top"></td>
<td rowspan="3" align="left" valign="top"><img src="./picture/fe.jpg" width="5" height="242"></td>
<td> </td>
</tr>
<tr>
<td rowspan="5" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td height="23"> </td>
<td> </td>
</tr>
<tr>
<td height="30"> </td>
<td align="right" valign="top"><img src="./picture/fn.jpg" width="450" height="5"></td>
<td> </td>
</tr>
<tr>
<td height="15"> </td>
<td colspan="2" valign="bottom"><span class="style3">  产品介绍</td>
<td> </td>
</tr>
<tr>
<td height="232"></td>
<td valign="top"></td>
<td rowspan="2" align="left" valign="top"><img src="./picture/fe.jpg" width="5" height="242"></td>
<td></td>
</tr>
<tr>
<td height="10"></td>
<td align="right" valign="top"></td>
<td></td>
</tr>
</table></td>
<td width="46"> </td>
</tr>
<tr>
<td height="9"></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
<%@ include file="../footer.jsp"%>

JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
可以看看你的全部代码吗?你这样写没有错误的啊,按照这个写法
String user=request.getParameter("user");//取的user的值
user = user.trim();//去掉空格
可以取到你的user的值的。
麻烦你把你的上级网页代码全部贴出来我看看好吗?
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
错了!
应该是:
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">用 户</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="text" name="user" size="14"></td>
</tr>
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">密 码</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="password" name="password" size="14"></td>
</tr>
<tr> </tr>
</table>
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" height="30" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="regist.jsp"><img src="./picture/zhuce_off.gif" border="0"></a></td>
<td width="10%" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="/Exec/servlet/login.Login_server"><img src="./picture/denglu_off.gif" border="0"></a></td>
</tr>
</table>
gwang820124 2004-09-23
  • 打赏
  • 举报
回复
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">用 户</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="text" name="user" size="14"></td>
</tr>
<tr>
<td width="42" height="20" align="right" valign="middle"><span class="style2">密 码</span></td>
<td width="3"> </td>
<td width="115" align="left" valign="middle"><input type="password" name="password" size="14"></td>
</tr>
<tr> </tr>
</table>
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" height="30" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="regist.jsp"><img src="./picture/zhuce_off.gif" border="0"></a></td>
<td width="10%" align="center" valign="bottom" bgcolor="#CCCCCC"><a href="/Exec/servlet/login.Login_server?user=<%=getParameter("user")%>"><img src="./picture/denglu_off.gif" border="0"></a></td>
</tr>
</table>
这是上一级网页的登录部分的代码!
JIEK_ONE 2004-09-23
  • 打赏
  • 举报
回复
String user=request.getParameter("user");//取的user的值
user = user.trim();//去掉空格
一般是这样写的
楼主把你的上级网页代码给大家看看OK??
加载更多回复(18)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧