当前位置: 技术问答>java相关
一个关于JavaBean的问题求教各位大侠,问题能解决200分相送!在线等待
来源: 互联网 发布时间:2015-11-07
本文导语: 我使用的是w2k+j2sdk1.4+tomcat4.1,mysql的jdbc驱动已经挂载并能正常使用,我刚学javabean,我自己写了一个bean的程序DataBean.java如下: import java.sql.*; import java.util.*; public class DataBean { private Connection conn=null; private Resu...
我使用的是w2k+j2sdk1.4+tomcat4.1,mysql的jdbc驱动已经挂载并能正常使用,我刚学javabean,我自己写了一个bean的程序DataBean.java如下:
import java.sql.*;
import java.util.*;
public class DataBean
{
private Connection conn=null;
private ResultSet rs=null;
public DataBean()
{
}
public Connection setConnection(String db)
{
StringTokenizer dbs=new StringTokenizer(db,",");
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://"+dbs.nextToken(),dbs.nextToken(),dbs.nextToken());
}
catch (Exception e)
{
System.out.println(e.toString());
}
return conn;
}
public ResultSet getQuery(String sqls)
{
try
{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqls);
}
catch (Exception e)
{
System.out.println(e.toString());
}
return rs;
}
public void setModify(String sqls)
{
try
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqls);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
然后写了test.jsp来测试调用这个bean:
JavaBean操作数据库测试
DataBean.java我已用javac命令成功编译出DataBean.class,且把它放在了tomcatsharedclasses中作为可共享调用的类,但当我运行test.jsp时就发生下面这些错误:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:43: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DataBean DatBean = null;
^
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:45: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DatBean = (DataBean) pageContext.getAttribute("DatBean", PageContext.PAGE_SCOPE);
^
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:48: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DatBean = (DataBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "DataBean");
^
3 errors
想了试了整整一天都毫无结果,也找不到相关的资料,我正准备做一个项目就是要用bean的,求各位高手大侠相助,问题能解决200分相赠!
import java.sql.*;
import java.util.*;
public class DataBean
{
private Connection conn=null;
private ResultSet rs=null;
public DataBean()
{
}
public Connection setConnection(String db)
{
StringTokenizer dbs=new StringTokenizer(db,",");
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://"+dbs.nextToken(),dbs.nextToken(),dbs.nextToken());
}
catch (Exception e)
{
System.out.println(e.toString());
}
return conn;
}
public ResultSet getQuery(String sqls)
{
try
{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqls);
}
catch (Exception e)
{
System.out.println(e.toString());
}
return rs;
}
public void setModify(String sqls)
{
try
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqls);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
然后写了test.jsp来测试调用这个bean:
JavaBean操作数据库测试
DataBean.java我已用javac命令成功编译出DataBean.class,且把它放在了tomcatsharedclasses中作为可共享调用的类,但当我运行test.jsp时就发生下面这些错误:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:43: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DataBean DatBean = null;
^
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:45: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DatBean = (DataBean) pageContext.getAttribute("DatBean", PageContext.PAGE_SCOPE);
^
An error occurred at line: 1 in the jsp file: /test.jsp
Generated servlet error:
C:Tomcat 4.1workStandalonelocalhostmyjsptest_jsp.java:48: cannot resolve symbol
symbol : class DataBean
location: class org.apache.jsp.test_jsp
DatBean = (DataBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "DataBean");
^
3 errors
想了试了整整一天都毫无结果,也找不到相关的资料,我正准备做一个项目就是要用bean的,求各位高手大侠相助,问题能解决200分相赠!
|
哇
怎么可以这么放啊!
如果你没有配置server.xml
那么就在默认文件夹下:
可以放在webapps下
建一个文件夹test
在test 下建
web-info文件夹
在web-info文件夹下
建classes文件夹
把你的bean放在里面就可以了
jsp文件放在test下就可以了
怎么可以这么放啊!
如果你没有配置server.xml
那么就在默认文件夹下:
可以放在webapps下
建一个文件夹test
在test 下建
web-info文件夹
在web-info文件夹下
建classes文件夹
把你的bean放在里面就可以了
jsp文件放在test下就可以了
|