为什么不能创建BEAN?(100分)

  • 主题发起人 梁玉双
  • 开始时间

梁玉双

Unregistered / Unconfirmed
GUEST, unregistred user!
我在JSP编程中遇到一个问题无法解决,请帮我看看:
在 myjsp.jsp 文件中引用一个BEAN文件:Database.java,服务器报错:Cann't create bean of class database.Database
若引用另外一个bean:mytest1.java 则执行成功,Database.java与mytest1.java 均能编译成功。
 
补充:
myjsp.jsp放在D:/jswdk-1.0.1/examples/jsp/database目录下
mytest1.java 、Database.java放在D:/jswdk-1.0.1/examples/WEB-INF/jsp/beans/database目录下。
 
能把Database.java贴一下吗?这样好分析分析
 

Database.java的程序如下
package javaBean ;
import java.io.Serializable;
import java.util.*;
import java.sql.*;
public class Database
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd= null;
/** Establishes a connection whith the dadabase server
* @param serverName
* @return (None)-constructor
* @exception SQLException if there is problem connecting
* to the server or accessing StockMarket database
*/
public Database (String serverName) throws SQLException
{
try
{
java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String connectString = "jdbc:eek:racle:thin:mad:10.1.1.14:1521:eek:ra8";
con = java.sql.DriverManager.getConnection (connectString,"zjgsmmis","zjgsmmis");
stmt = con.createStatement();
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
/** Close the connection with the database server
* @param none
* @return void
* @exception SQLException if there is a problom connecting
* to the server
*/
public void close() throws SQLException
{
try
{
if (rs != null)
{
rs.close();
}
if (con != null)
{
con.close();
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
/** get the dhhm
* @param String dhhm,String tablename
* @return int YHBH or int JSZH
* @exception SQLException,Exception
*/
public int getId(String dhhm,String tablename) throws SQLException ,Exception
{
int YHBH = 0 ;
int JSZH =0;
try
{
String sql="select YHBH,JSZH from TYT_YHDA where DHHM = "+dhhm;
rs=stmt.executeQuery(sql);
while (rs.next()){
YHBH=rs.getInt("YHBH");
JSZH=Integer.parseInt(rs.getString("JSZH"));
}
}
catch (SQLException e)
{
System.out.println(e);
}

catch (Exception e)
{
System.out.println(e);
}
if (tablename.equals("TYT_YHZH"))
{ return JSZH;
}else
{
return YHBH;
}
}


public void updateYhdb (int YHBH,String DBYB,
String DBHM,String DBTXDZ,int DBYJ ) throws RecordNotFoundException
{
if ( !haveRecord("TYT_YHDB",YHBH))
{
throw new RecordNotFoundException("用户编号不存在");
}
try
{

String sql = "update TYT_YHDB set DBYB = '"+DBYB+"', DBHM = '"+DBHM+"',DBTXDZ = '"+DBTXDZ+"',DBYJ = "+DBYJ+" where YHBH= '"+YHBH+"'";
stmt.executeUpdate(sql);
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}

/**return a customerRec populated with data from
* the Customer and Shares table
* @param String ssn
* @return CustomerRec
* @exception RecordNotFoundException if ssn is not found
* in the Customer table
*/

//YhdbRec 是自己定义的类,是一个结果集
public YhdbRec getYhdb(int YHBH) throws
RecordNotFoundException
{
YhdbRec YhdbR=null;
try
{
if (!haveRecord("TYT_YHDB",YHBH))
{
throw new RecordNotFoundException("记录不存在!");
}
String sql = "select * from TYT_YHDB where YHBH = "+YHBH;
rs = stmt.executeQuery(sql);
while (rs.next())
{
YhdbR = new YhdbRec(rs.getInt("YHBH"),rs.getString("DBYB"),rs.getString("DBHM"),rs.getString("DBLXDH"),rs.getString("DBTXDZ"),rs.getInt("DBYJ"));
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
return YhdbR;

}



public YhxxRec getYhxx(int YHBH) throws
RecordNotFoundException
{
YhxxRec YhxxR=null;
try
{
if (!haveRecord("TYT_YHXX",YHBH))
{
throw new RecordNotFoundException("记录不存在!");
}
String sql = "select * from TYT_YHXX where YHBH = "+YHBH;
rs = stmt.executeQuery(sql);
while (rs.next())
{
YhxxR = new YhxxRec(rs.getInt("YHBH"),rs.getString("ZJDZ"),rs.getDate("FZRQ"),rs.getInt("YXQX"),rs.getString("LXDH"),rs.getString("DWYB"),rs.getString("DWHM"),rs.getString("DWLXDH"),rs.getString("DWTXDZ"));
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
return YhxxR;

}



private boolean haveRecord(String tableName,int YHBH) {
boolean have = false;
try
{
//stmt = con.createStatement();
String sql="";
if (tableName.equals("TYT_YHZH"))
{
sql = "select * from TYT_YHZH where JSZH = '" +
YHBH+"'";
}else
{
sql="select * from " + tableName +
" where YHBH='" +YHBH +"'" ;
}
rs=stmt.executeQuery(sql);
have=rs.next();
//System.out.println(have);
rs.close();

}catch(SQLException e) {
System.out.println(e);
}
return have;
}
};



 
问题还没解决吗?
建议你不要在构造函数里做太多的东西
bean无法创建有可能是构造函数异常
 
如果你不介意,你的包错了。在你的程序你,引用的包错了,程序里是:database.Database
,而定义是javaBeans.Database,肯定是找不到 class的错误嘛。
先学JAVA,再学JSP
 
yes, just replace ur package with database or change the directory to javaBean
is ok then
.
 
我在引用Database.java时改了包名
<jsp:useBean id='conne' scope='request' class='javaBean.Database' type="javaBean.Database" />
可是运行时出现Cann't create bean of class javaBean.Database的错误。
 
你就当在头里引用:如下:
<%@ page import='javaBean.Databas'%>
并且将这个包放到你的 servlet 所指向的目录。
如果是用的 resin,是WEB-INFO/classes,如果还不对,
你的程序可能,或者是环境可能有问题。
 
我现在用的就是<%@ page import='javaBean.Databas'%>这种方法,运行正常,但是我
不懂为什么用<jsp:useBean id='conne' scope='request' class='javaBean.Database' type="javaBean.Database" />
就不可以?
 
给你一点分吧。你应该承认我是给你正确答案的。我 很穷。
 
多人接受答案了。
 
顶部