您的位置:程序门 -> java -> web 开发



javabean使用有问题 各位大侠帮忙!烦死我了!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


javabean使用有问题 各位大侠帮忙!烦死我了![已结贴,结贴人:programmebird]
发表于:2007-10-11 20:24:06 楼主

connectdb

package   bbs;

import   java.lang.*;
import   java.sql.*;

public   class   connectdb   {
        string   url="jdbc:odbc:bbs";
        connection   dbconn;
        string   errmes;
        public   connectdb()
        {
                errmes="";
                dbconn=null;
        }
        public   connection   getconn()
        {
                try{
                        class.forname("sun.jdbc.odbc.jdbcodbcdriver");
                        dbconn=drivermanager.getconnection("jdbc:odbc:bbs","sa","");
                }catch(exception   e){
                        system.out.print(e);
                }
                return   dbconn;
        }
        public   string   geterrmes()
        {
                return   errmes;
        }
}

package   bbs;

import   java.lang.*;
import   java.sql.*;

public   class   EXECutedb   extends   connectdb   {
        private   connection   dbconn;
        private   statement   stmt;
        private   resultset   rs;
        private   string   errmes;
        public   EXECutedb()   {
                dbconn=super.getconn();
                stmt=null;
                rs=null;
                this.errmes=super.geterrmes();
        }
        public   boolean   exesql(string   strsql)
        {
                boolean   issuc=false;
                try
                {
                        stmt=dbconn.createstatement();
                        stmt.EXECuteupdate(strsql);
                        stmt.close();
                        issuc=true;
                }
                catch(exception   e)
                {
                        this.errmes=this.errmes+" <br> "+e.tostring();
                }
                return   issuc;
        }
        public   resultset   exequery(string   strsql)
        {
                try
                {
                        stmt=dbconn.createstatement();
                        rs=stmt.EXECutequery(strsql);
                }
                catch(exception   e)
                {
                        this.errmes=this.errmes+" <br> "+e.tostring();
                        rs=null;
                }
                return   rs;
        }
        public   string   geterrmes()
        {
                return   errmes;
        }
}

package   bbs;

import   java.sql.*;

public   class   user   extends   EXECutedb{
        private   long   userid;
        private   string   username;
        private   string   userpassword;
        private   string   email;
        private   string   sex;
        private   string   nickname;
        private   string   createtime;
        private   string   role;
        private   string   strsql;
        public   user()
        {
                super();
                this.userid=0;
                this.username="";
                this.userpassword="";
                this.sex="";
                this.nickname="";
                this.role="0";
                this.email="";
                java.util.date   nowtime=new   java.util.date();
                this.createtime=nowtime.tostring();
                this.strsql="";
        }
        public   void   setusername(string   name)
        {
                this.username=name;
        }
        public   void   setuserpassword(string   password)
        {
                this.userpassword=password;
        }
        public   void   setsex(string   sex)
      {
              this.sex=sex;
      }
      public   void   setnickname(string   nickname)
      {
              this.nickname=nickname;
      }
      public   void   setemail(string   email)
      {
              this.email=email;
      }


        public   boolean   add()
        {
                this.strsql="insert   into   users   (username,userpassword,email,sex,nickname,createtime)   ";
                this.strsql="   values('"+username+"','"+userpassword+"','"+email+"','"+sex+"','"+nickname+"')";
                boolean   isadd=super.exesql(this.strsql);
                return   isadd;
        }
        public   boolean   modify_info()
        {
                this.strsql="update   users   set";
                this.strsql=this.strsql+"email="+"'"+this.email+"',";
                this.strsql=this.strsql+"sex="+"'"+this.sex+"',";
                this.strsql=this.strsql+"nickname="+"'"+this.nickname+"',";
                this.strsql=this.strsql+"create="+"'"+this.createtime+"',";
                this.strsql=this.strsql+"where   userid='"+this.userid+"'";
                boolean   isupdate=super.exesql(this.strsql);
                return   isupdate;
        }
        public   boolean   modify_userpassword()
        {
                this.strsql="update   users   set";
                this.strsql=this.strsql+"userpassword="+"'"+this.userpassword+"'";
                this.strsql=this.strsql+"where   userid='"+this.userid+"'";
                boolean   isupdatekey=super.exesql(this.strsql);
                return   isupdatekey;
        }
        public   boolean   init()
        {
                this.strsql="select   *   from   'users'   where   userid=";
                this.strsql=this.strsql+"'"+this.userid+"'";
                try
                {
                        resultset   rs=super.exequery(this.strsql);
                        if(rs.next())
                        {
                                this.userid=rs.getlong("userid");
                                this.username=rs.getstring("username");
                                this.userpassword=rs.getstring("userpassword");
                                this.sex=rs.getstring("sex");
                                this.nickname=rs.getstring("nickname");
                                this.email=rs.getstring("email");
                                this.createtime=rs.getstring("createtime");
                                return   true;
                        }
                        else
                        {
                                return   false;
                        }
                }catch(exception   e)
                {
                        system.out.println(e.tostring());
                        return   false;
                }
        }
        public   boolean   valid()
        {
                this.strsql="select   userid,username,role   from   'users'";
                this.strsql=this.strsql+"   where   username='"+this.username+"'";
                this.strsql=this.strsql+"   and   userpassword='"+this.userpassword+"'";
                system.out.println(this.strsql);
                try
                {
                        resultset   rs=super.exequery(this.strsql);
                        if(rs.next())
                        {
                                this.userid=rs.getlong("userid");
                                this.username=rs.getstring("username");
                                this.role=rs.getstring("role");
                                return   true;
                        }
                        else
                        {
                                return   false;
                        }
                }catch(exception   ex)
                {
                        return   false;
                }
        }
        public   boolean   exist()
        {
                this.strsql="select   *   from   'users'   ";
                this.strsql=this.strsql+"   where   username='"+this.username+"'";
                try
                {
                        resultset   rs=super.exequery(this.strsql);
                        if(rs.next())
                        {
                                return   true;
                        }
                        else
                        {
                                return   false;
                        }
                }catch(exception   exe)
                {
                        system.out.println(exe.tostring());
                        return   false;
                }
        }
}

在调用add方法时候出错...
发表于:2007-10-11 20:26:261楼 得分:0
处理注册信息的界面
<%@   page   contenttype="text/html;   charset=gbk"%>
<jsp:usebean   id="bean0"   scope="session"   class="bbs.user"   />
<jsp:setproperty   name="bean0"   property=""/>
<html>
<!doctype   html   public   "-//w3c//dtd   xhtml   1.0   transitional//en"   "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html   xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta   http-equiv="content-type"   content="text/html;   charset=gb2312"   />
<title> 无标题文档 </title>
</head>

<body   bgcolor="#ffffff">
<div   align="center">
<h2> 论坛注册 </h2>
<%
boolean   issuc=false;
request.setcharacterencoding("gbk");
response.setcharacterencoding("gbk");
string   srand1=(string)session.getattribute("rand");
string   srand2=request.getparameter("rand");

if(srand1.compareto(srand2)==0)
{
    string   susername=request.getparameter("username");
    if(bean0.exist())
    {
        out.print("用户名已经被注册!");
    }
    else
    {
        string   suserpassword=request.getparameter("usetpassword");
        string   snickname=(string)request.getparameter("nickname");
        string   ssex=request.getparameter("sex");
        string   semail=request.getparameter("email");
        bean0.setuserpassword(suserpassword);
        bean0.setnickname(snickname);
        bean0.setsex(ssex);
        bean0.setemail(semail);
        if(bean0.add())
        {
            issuc=true;
            out.print("用户注册成功!");
        }
        else
        {
            out.print("用户注册失败,请稍后再试!");
        }
    }
}
else
{
    out.print("验证码错误!");
}
if(issuc)
{
    out.print("登录成功!");
}
else
{
    out.print("错误!");
    session.removeattribute("rand");
}
%>
</div>
</body>
</html>


提示的错误~
http   status   500   -   type   exception   reportmessage   description   the   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.exception   org.apache.jasper.jasperexception:   cannot   find   any   information   on   property   ''   in   a   bean   of   type   'bbs.user'
  org.apache.jasper.runtime.jspruntimelibrary.internalintrospecthelper(jspruntimelibrary.java:363)
  org.apache.jasper.runtime.jspruntimelibrary.introspecthelper(jspruntimelibrary.java:306)
  org.apache.jsp.user_005freg_005fsave_jsp._jspservice(org.apache.jsp.user_005freg_005fsave_jsp:54)
  org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:97)
  javax.servlet.http.httpservlet.service(httpservlet.java:802)
  org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:322)
  org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:291)
  org.apache.jasper.servlet.jspservlet.service(jspservlet.java:241)
  javax.servlet.http.httpservlet.service(httpservlet.java:802)
note   the   full   stack   trace   of   the   root   cause   is   available   in   the   apache   tomcat/5.5.9   logs.apache   tomcat/5.5.9

请大家帮忙啊
发表于:2007-10-11 23:12:032楼 得分:3
找不到bbs.user
发表于:2007-10-11 23:20:133楼 得分:3
<jsp:setproperty   name="bean0"   property=""/   >
应该改为
<jsp:setproperty   name="bean0"   property="*"/   >
发表于:2007-10-12 08:30:484楼 得分:0
谢谢各位   我去试试
发表于:2007-10-12 21:18:475楼 得分:0
改了 <jsp:setproperty   name="bean0"   property="*"/     >
但是有错误提示nullpointexception   不知道是哪里错误
发表于:2007-10-12 22:20:036楼 得分:3
有个小错误    

this.strsql="   values(   '"+username+"   ',   '"+userpassword+"   ',   '"+email+"   ',   '"+sex+"   ',   '"+nickname+"   ')";


应该是  

this.strsql+=.................吧  
发表于:2007-10-13 08:46:377楼 得分:1
顶,楼上的
发表于:2007-10-13 09:20:578楼 得分:0
哦   ?   我一新手     我试试去  
发表于:2007-10-13 09:27:089楼 得分:0
依然是java.lang.nullpointerexception
我添加信息的方法是这么写的
  public   boolean   add()
        {
                this.strsql="insert   into   users   (username,userpassword,email,sex,nickname,createtime)   ";
                this.strsql=this.strsql+"   values('"+username+"','"+userpassword+"','"+email+"','"+sex+"','"+nickname+"','"+createtime+"')";
              boolean   isadd=super.exesql(this.strsql);
                return   isadd;
        }
处理界面里是这么写
bean0.setusername(susername);
        string   suserpassword=request.getparameter("usetpassword");
        string   snickname=(string)request.getparameter("nickname");
        string   ssex=request.getparameter("sex");
        string   semail=request.getparameter("email");
        bean0.setuserpassword(suserpassword);
        bean0.setnickname(snickname);
        bean0.setsex(ssex);
        bean0.setemail(semail);
        if(bean0.add())
        {
            issuc=true;
            out.print("用户注册成功!");
        }
        else
        {
            out.print("用户注册失败,请稍后再试!");
        }
但总是失败
发表于:2007-10-13 14:53:2010楼 得分:5
把bean中的属性首字母全改成小写,就搞定了,记得结帖...要不然下次不告诉你了   呵呵
        private   long   userid;  
        private   string   username;  
        private   string   userpassword;  
        private   string   email;  
        private   string   sex;  
        private   string   nickname;  
        private   string   createtime;  
        private   string   role;  
        private   string   strsql;  
发表于:2007-10-14 10:44:2011楼 得分:0
不会吧   这也是个问题?
发表于:2007-10-14 18:39:2212楼 得分:3
当然是个问题..get/set方法会把你bean属性中的首字母变大写..所以你get不到   当然add不进去了..
结贴.......
发表于:2007-10-14 19:15:5813楼 得分:0
是小写   我确定  
发表于:2007-10-14 19:16:5114楼 得分:2
你帖出来的代码不都是大写么?
发表于:2007-10-15 08:44:4415楼 得分:0
小写的...我确定!


快速检索

最新资讯
热门点击