以下是一个关于注册用户的页面,可是不管是否输入内容它总是写到数据库中。请高手看看哪有问题。
<%@ include file="convert.jsp"%>
<%
String username=Bytes(request.getParameter("username"));
String password=Bytes(request.getParameter("password"));
String sex=Bytes(request.getParameter("sex"));
String age=Bytes(request.getParameter("age"));
boolean notkong=true;
Connection conn=null;
Statement stmt=null;
int rs;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String path="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bin";
String struser="sa";
String pwd="";
conn=DriverManager.getConnection(path,struser,pwd);
stmt=conn.createStatement();
if (username!=null && username!="")
{
String sql="insert into users values('"+username+"','"+password+"','"+sex+"','"+age+"')";
rs=stmt.executeUpdate(sql);
}
}
catch(SQLException e)
{
out.print(e.getMessage());
}
catch(Exception e)
{
out.print(e.getMessage());
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(Exception e)
{
}
}
%>
convert.jsp内容为下
<%!
public String Bytes(String str)
{
try
{
String strOld=str;
byte[] strNew=strOld.getBytes("ISO8859-1");
String bytes=new String(strNew);
return bytes;
}
catch(Exception e){}
return null;
}
%>
<%@ include file="convert.jsp"%>
<%
String username=Bytes(request.getParameter("username"));
String password=Bytes(request.getParameter("password"));
String sex=Bytes(request.getParameter("sex"));
String age=Bytes(request.getParameter("age"));
boolean notkong=true;
Connection conn=null;
Statement stmt=null;
int rs;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String path="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bin";
String struser="sa";
String pwd="";
conn=DriverManager.getConnection(path,struser,pwd);
stmt=conn.createStatement();
if (username!=null && username!="")
{
String sql="insert into users values('"+username+"','"+password+"','"+sex+"','"+age+"')";
rs=stmt.executeUpdate(sql);
}
}
catch(SQLException e)
{
out.print(e.getMessage());
}
catch(Exception e)
{
out.print(e.getMessage());
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(Exception e)
{
}
}
%>
convert.jsp内容为下
<%!
public String Bytes(String str)
{
try
{
String strOld=str;
byte[] strNew=strOld.getBytes("ISO8859-1");
String bytes=new String(strNew);
return bytes;
}
catch(Exception e){}
return null;
}
%>
解决方案 »
- ajax读取xml。。。后台处理必须生成实体xml吗
- select 下拉菜单的问题
- 用dreamweaver编的JSP文件能否直接放在netbeans里面运行?
- 请教:同一台JSP 服务器,如何同时挂两个域名?也就是一台服务器如何能够同时运转一个以上网站
- 今起开始找工作!!!!
- 问个简单的问题:在Tomcat 4.1中class文件应该放在哪里?
- ie中如何获得windows的底层消息
- jsp页面中可否直接使用javascript的代码?
- Excel导入mysql数据库的时间类型:Date 类型 导入报错,求大神指导
- 怎么写提交<html:form>表单的javascript语句,我不想用<html:submit value="submit" />提交。
- Linux下安装mysql出问题
- JSP 显示得到的类
{
String sql="insert into users values('"+username+"','"+password+"','"+sex+"','"+age+"')";
rs=stmt.executeUpdate(sql);
}那就是说if的结果是true咯。改成
if (username!=null && !"".equals(username.trim()))
{
String sql="insert into users values('"+username+"','"+password+"','"+sex+"','"+age+"')";
rs=stmt.executeUpdate(sql);
}
试试,不行那就是你的Bytes()函数有问题了。
ps:这代码蛮烂的。