Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A question about jsp+javabean.  RSS feed

 
bowlder shimin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have downloaded a jsp+bean programme.The bean programme is right running on java editor,the "checkUser()" return is false. But in jsp the result is error,the "checkUser()" return is true.The parameters of input are same.I don't know why.
My English is poor,I hope you can understand me.Thank you!
The programme is:
//-----------------------------------------db.java
package db;
/**
* A Class class.
* <P>
* @author liuyufeng
*/
//声明类库文件
import oracle.jdbc.driver.*;
import java.net.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class db
{//成员变量初始化
Connection conn = null; //数据库连接,如同asp中所写的include文件
ResultSet rs = null; //记录集,set rs = Server.CreateObject("ADODB.Recordset")
String username=""; //用户名
String password=""; //密码
String email=""; //email
String homepage=""; //主页
String signs=""; //签名
//db的构建器
public db()
{
try
{
//注册数据库驱动程序为Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
//违例处理。这样写是为了方便调试程序,出错打印my db()就知道在什么地方出错了
System.err.println("mydb(): " + e.getMessage());
}
}
//executeQuery方法用于进行记录的查询操作
//入口参数为sql语句,返回ResultSet对象
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
//建立数据库连接,使用Oracle的一种thin连接方式,demo为主 426;名字,demodb为数据库,后面的两个demo为用户名和密码
conn = DriverManager.getConnection("jdbc racle:thin:@shimin:1521 racle","test","test");
Statement stmt = conn.createStatement();
//执行数据库查询操作
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("db.executeQuery: 123456" + ex.getMessage());
}
return rs;
}
//executeUpdate方法用于进行add或者update记录的操作
//入口参数为sql语句,成功返回true,否则为false
public boolean executeUpdate(String sql)
{
boolean bupdate=false;
rs = null;
try
{
//建立数据库连接,其它参数说明同上面的一样
conn = DriverManager.getConnection("jdbc racle:thin:@shimin:1521 racle","test","test");
Statement stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
//如果不成功,bupdate就会返回0
if(rowCount!=0)bupdate=true;
}
catch(SQLException ex)
{
//打印出错信息
System.err.println("db.executeUpdate: " + ex.getMessage());
}
return bupdate;
}
//toChinese方法用于将一个字符串进行中文处理
//否则将会是???这样的字符串
public static String toChinese(String strvalue)
{
try
{
if(strvalue==null)
{
return null;
}
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch(Exception e)
{
return null;
}
}
/*
public static void main(String args[])
{
ResultSet rset = new db().executeQuery ("select * from test");
// Iterate through the result and print the employee names
try
{
while (rset.next ())
System.out.println (rset.getString (1));
}
catch(SQLException ex)
{
System.err.println("db.executeQuery.next: " + ex.getMessage());
}

}
*/
}

//-----------------------------------------adduser.java
package db;
/**
* A Class class.
* <P>
* @author liuyufeng
*/
//导入java类库
import java.sql.*;
import java.lang.*;
import oracle.jdbc.driver.*;
//adduser由db派生出来,拥有db的成员变量和方法
public class adduser extends db
{
//构建器
public adduser(){}
public boolean addNewUser()
{
boolean boadduser=false;
try
{
//进行用户注册的记录添加操作,生成sql语句
String sSql=new String("insert into test(regtime,username,password,email,homepage,signs)");
sSql=sSql+" values(SYSDAYE,'"+username+"','"+password+"','"+email+"','"+homepage+"','"+signs+"')";
//一种调试的方法,可以打印出sql语句,以便于查看错 ;误
System.out.println(sSql);
//调用父类的executeUpdate方法,并根据成功以否来设置返回Í 40;
//if(super.executeUpdate(sSql))boadduser=true;
}
catch(Exception ex)
{
//出错处理
System.err.println("adduser.addNewUser: " + ex.getMessage());
}
finally
{
//无论是否出错,都要返回值
return boadduser;
}
}
//checkUser()方法用来检查用户名是否重复
//如果重复返回一个false
public String checkSql()
{return "select * from test where username='"+username+"'";
}
public boolean checkUser()
{
boolean boadduser=true;
try
{
//构建sql查询语句
String sSql="select * from test where username='"+username+"'";
System.out.println(sSql);
//调用父类的executeQuery方法
if((super.executeQuery(sSql)).next())
{
//查询出来的记录集为空
boadduser=false;
}
else
{
boadduser=true;
}
}
catch(Exception ex)
{
//出错处理
System.err.println("adduser.addNewUser: " + ex.getMessage());
}
finally
{
//返回值
return boadduser;
}
}
//属性的set/get方法,同请求的参数一致
/*
其实下面的所有get/set方法都是重复性劳动,为了避免ŵ 25;复性的拷贝粘贴工作,我写了个软件Jsp Code Faster,只要输入一系列的字段名,所有的get/set方法都可以 ;自动生成,大家可以在我的网站http://jspbbs.yeah.net上下载这个ů 19;件
*/
//属性用户名username的get/set方法
public String getUsername()
{
return username;
}
public void setUsername(String newUsername)
{
//用户名有可能是中文,需要进行转换
//username =db.toChinese(newUsername);
username =newUsername;
}
//属性密码password的get/set方法
public String getPassword()
{
return password;
}
public void setPassword(String newPassword)
{
password = newPassword;
}
//属性email的get/set方法
public String getEmail()
{
return email;
}
public void setEmail(String newEmail)
{
email = newEmail;
}
//属性主页homepage的get/set方法
public String getHomepage()
{
return homepage;
}
public void setHomepage(String newHomepage)
{
homepage = newHomepage;
}
//属性主页signs的get/set方法
public String getSigns()
{
return signs;
}
public void setSigns(String newSigns)
{
//签名有可能是中文,需要进行转换
//signs = db.toChinese(newSigns);
signs = newSigns;
}

public static void main(String args[])
{
adduser rset = new adduser();
rset.setUsername("test");
//rset.executeQuery ("select * from test");
System.out.println("返回:"+rset.getUsername()+".");
System.out.println("结果:"+rset.checkUser());
// Iterate through the result and print the employee names
/*
try
{
while (rset.next ())
System.out.println (rset.getString (1));
}
catch(SQLException ex)
{
System.err.println("db.executeQuery.next: " + ex.getMessage());
}
*/
}

}
//-----------------------------------------user.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<% response.setHeader("Expires","0"); %>
<html>
<head>
<title>新用户注册</title>
</head>
<body bgcolor="#FFFAD9">
<p align="center"><font color="#8484FF"><span class="main1"><strong><big>新个人用户注册</big></strong></span></font></p>
<span class="main1">
<form method="POST" name="formreg" action="douser.jsp">
<div align="center">
<center>
<table border="1" width="50%" borderColorDark="#FFFFFF" borderColorLight="#000000" cellspacing="0">
<tr>
<td width="27%" bgcolor="#DDDDFF" align="center"><span class="main1">用户名:</td>
<td width="73%" bgcolor="#DDDDFF"><span class="main1"><input type="text" name="username" size="20" tabindex="1"><font color="#FF0000"><b>*</b></font></span></td>
</tr>
<tr>
<td width="30%" bgcolor="#DDDDFF" colspan="2">
<div align="center"><center><p>
<input type="submit" value="递交" name="B1" tabindex="10">
<input type="reset" value="清除" name="B2" tabindex="11">
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
//-----------------------------------------douser.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<% response.setHeader("Expires","0"); %>
<!--生成一个JavaBean:lyf.adduser的实例,id为adduser,生存范围为page-->
<jsp:useBean id="adduser" class="db.adduser" scope="page"/>
<!--设置JavaBean中各个属性的值,这会调用JavaBean中各个属性į 40;set方法,以便JavaBean得到正确的属性值,”*”代表进行所 ;有属性的匹配-->
<jsp:setProperty name="adduser" property="username"/>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>用户添加</title>
</head>
<body bgcolor="#FFEBBD">
<div align="center"><center>
<%
//调用lyf.adduser的checkUser()方法检查是否有重复的用户名
//如果有重复就显示对应的信息
out.println(adduser.checkUser()+"用户名"+adduser.getUsername()+"!"+adduser.checkUser());
out.println(adduser.checkSql());
/*
if(!adduser.checkUser())
{
//页面文字输出信息,使用jsp内置对象out的println方法,相ô 03;于asp中的response.write方法
out.println("对不起,这个用户名"+adduser.getUsername()+"已经被申请了,请ດ 5;新选择!");
//return代表返回,运行时候碰到return就不会进行下面的处ச 2;了,功能相当于asp中的response.end
//return;
}
*/
%>
</BODY>
</HTML>
[ March 20, 2002: Message edited by: Madhav Lakkapragada ]
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bowlder
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you have some foreign characters in your post, please post only the relavent code or edit your post accordingly.
Also, I edited it and disabled the smiles, but couldn't do anything about the foreign chars.
- satya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!