javaBean练习
<%@ page contentType= "text/html;charset=GB2312 "%>
<%@ include file= "common.jsp " %>
<%@ page import = "java.io.* "%>
<%@ page import = "java.util.* "%>
<%@ page import = "java.sql.* "%>
<jsp:useBean id= "DB " scope = "application " class= "ConBook "/>
<%
try{
Connection con = DB.getConnection();
Statement stmt;
ResultSet rs;
// Class.forName( "com.mysql.jdbc.Driver ");
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// String dburl = "jdbc:mysql://localhost:3306/BookDB ";
// String dbUser= "dbuser ";
// String dbPwd= "1234 ";
// con = java.sql.DriverManager.getConnection(dburl,dbUser,dbPwd);
stmt = con.createStatement();
rs = stmt.executeQuery( "select id,name,title,price from books ");
out.println( " <table border =1 width=400> ");
while(rs.next()){
String col1 = convert(rs.getString(1));
String col2 = convert(rs.getString(2));
String col3 = convert(rs.getString(3));
float col4 = rs.getFloat(4);
out.println( " <tr> <td> "+col1+ " </td> <td> "+col2+ " </td> <td> "+col3+ " </td> <td> "+col4+ " </td> </tr> ");
}
out.println( " </table> ");
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /index.jsp
ConBook cannot be resolved to a type
5: <%@ page import = "java.sql.* "%>
6:
7:
8: <jsp:useBean id= "DB " scope = "application " class= "ConBook "/>
9:
10:
11: <%
ConBook是个数据库连接类,已经编译好了,放在WEN-INF/classes 下。
------解决方案--------------------加上包结构
没有包的类 是不能被有包的类访问的
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html