一个注册程序,搞的我头都大了,高手进来帮帮忙

szm629 2004-01-16 12:40:38
一登陆页面就出现“用户已存在",然后不能判断年龄在"0-120"之间,估计是与数据库连接异常才出现“用户已存在",
下面是Bean文件
package tom.jiafei;
import java.sql.*;

public class Register
{
String logname=null,
password=null,
sex=null,age=null,
email=null,
phone=null,
address=null,
message=null;
String backNews;
Connection con=null;
Statement sql=null;
public Register()
{
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
}
//茯得属性值
public void setLogname(String log)
{
if(log!=null)
{
this.logname=log;
}
}
public void setPassword(String pass)
{
if(pass!=null)
{
this.password=pass;
}
}
public void setSex(String s)
{
if(s!=null)
{
this.sex=s;
}
}
public void setAge(String a)
{
if(a!=null)
{
this.age=a;
}
}
public void setEmail(String e)
{
if(e!=null)
{
this.email=e;
}
}
public void setPhone(String p)
{
if(p!=null)
{
this.phone=p;
}
}
public void setAddress(String add)
{
if(add!=null)
{
this.address=add;
}
}
public void setMessage(String mess)
{
if(mess!=null)
{
this.message=mess;
}
}
//添加记录到数据库
public String addItem()
{
try
{
con=DriverManager.getConnection("jdbc:odbc:szm","sa","");
sql=con.createStatement();
String s="'"+logname+"'"+","+"'"+password+"'"+","+"'"+sex+"'"+","+"'"+age+"'"+","+"'"+phone+"'"+","+"'"+email+"'"+","+"'"+address+"'"+","+"'"+message+"'";
String condion="insert into member values"+"("+"s"+")";
sql.executeUpdate(condion);
backNews="注册成功";
con.close();
}
catch(SQLException e)
{
backNews="用户已存在";
}
return backNews;
}
}
***************************************************
下面是jsp文件

<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="tom.jiafei.Register"%>
<jsp:useBean id="file" class="tom.jiafei.Register" scope="session">
</jsp:useBean>
<%!
public String getString(String s)
{
String are=s;
try
{
byte b[]=are.getBytes("ISO-8859-1");
are=new String(b);
}
catch(Exception e)
{}
return are;
}
%>
<html>
<body><font size=1>
<%@ include file="head.txt"%>
</font>
<Center><p>输入您的信息,带*的必须填写</Center>
<p>
<form action="" method="post">
<p>会员名称: <input type="text" name="logname"> *
<p>设置密码: <input type="password" name="password"> *
<p>确认密码: <input type="password" name="pass"> *
<p>性 别: <select name="sex" >
<Option value="男">男
<Option value="女">女
</select>
<p>年 龄: <input type="text" name="age"> *
<p>电子邮件: <input type="text" name="email"> *
<p>联系电话: <input type="text" name="phone">
<p>通信地址: <input type="text" name="address">
<p><p>输入您的简介
<p><textArea name="message" Rows="8" Cols="57"></textArea>
<br><input type=submit value="提交">
</form>
<%--提交信息--%>
<%
String logname="",password="",pass="",sex="",age="",email="",
phone="",address="",message="";
logname=request.getParameter("logname");
password=request.getParameter("password");
pass=request.getParameter("pass");
sex=request.getParameter("sex");
age=request.getParameter("age");
email=request.getParameter("email");
phone=request.getParameter("phone");
address=request.getParameter("address");
message=request.getParameter("message");

if(logname==null){logname="";}
if(password==null){password="";}
if(pass==null){pass="";}
if(sex==null){sex="";}
if(age==null){age="";}
if(email==null){email="";}
if(phone==null){phone="";}
if(address==null){address="";}
if(message==null){message="";}

logname=getString(logname);
password=getString(password);
pass=getString(pass);
sex=getString(sex);
age=getString(age);
email=getString(email);
phone=getString(phone);
address=getString(address);
message=getString(message);

%>
<%--检查提交的信息--%>
<%
boolean b=false;
if(!(session.isNew()))
{
int n=0;
try{n=Integer.parseInt(age);}
catch(NumberFormatException e){n=0;}
if(logname.equals("")){out.print("<br>姓名不能为空");}
else if(password.equals("")){out.print("<br>密码不能为空");}
else if(age.equals("")){out.print("<br>年龄不能为空");}
else if((n>120) && (n<0)){out.print("<br>输入的年龄不正确");}
else if(email.equals("")){out.print("<br>邮件地址不能为空");}
else if(password.equals(pass))
{
b=true;
}
else
{
out.print("<br>输入的密码不正确");
}
}
if(b)
{%>
<jsp:setProperty name="file" property="logname" value="<%=logname%>" />
<jsp:setProperty name="file" property="password" value="<%=password%>" />
<jsp:setProperty name="file" property="sex" value="<%=sex%>" />
<jsp:setProperty name="file" property="age" value="<%age%>" />
<jsp:setProperty name="file" property="email" value="<%=email%>" />
<jsp:setProperty name="file" property="phone" value="<%phone%>" />
<jsp:setProperty name="file" property="address" value="<%address%>" />
<jsp:setProperty name="file" property="message" value="<%=message%>" />
<%}%>
<%String s=file.addItem();%>
<%=s%>


</body>
</html>



...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zkhj 2004-01-16
  • 打赏
  • 举报
回复
肚子搞大了才对!
mxlmwl 2004-01-16
  • 打赏
  • 举报
回复
没有你这个用法吧??发生异常的错误会有很多,你凭什么判断一出错就是用户已经存在啊,假如数据库关闭了,也是这样,你还说“用户已经存在”,是不是太搞笑了???
con=DriverManager.getConnection("jdbc:odbc:szm","sa","");
sql=con.createStatement();
String s="'"+logname+"'"+","+"'"+password+"'"+","+"'"+sex+"'"+","+"'"+age+"'"+","+"'"+phone+"'"+","+"'"+email+"'"+","+"'"+address+"'"+","+"'"+message+"'";
String condion="insert into member values"+"("+"s"+")";
sql.executeUpdate(condion);
backNews="注册成功";
con.close();
这段话中,你先判断有没有连上数据库,然后判断游标createStatement();创建是否正确,然后判断sql语句是否正确,依次来判断看看吧。注释掉一段代码,通过后,打开另一段,再试,一直试出错误为止
liutang2 2004-01-16
  • 打赏
  • 举报
回复
你的catch用的不对啊,好象没这种用法吧,
我建议你还是用个select看是否存在,如果存在就不要执行insert而是给出一存在的提示啊
czhcc 2004-01-16
  • 打赏
  • 举报
回复
我觉得用熟了STRUTS后,象这种写法不会看了。

81,094

社区成员

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

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