首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 其他开发语言 >

jsp网页设计解决办法

2013-09-11 
jsp网页设计JDBConnection.java代码如下package comimport java.sql.*public class JDBConnection{priva

jsp网页设计
JDBConnection.java代码如下


package com;
import java.sql.*;
public class JDBConnection{

private final String dbDriver=
  "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接sql数据库的方法

private final String url=
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02";
private final String userName="sa";
private final String password="";

private Connection con=null;

public JDBConnection(){

try{
Class.forName(dbDriver).newInstance(); //加载数据库驱动
}
catch(Exception ex){
}
}



private boolean creatConnection()
{
try{
 
con=DriverManager.getConnection(url,userName,psaaword);
con.setAutoCommit(true);
}catch(SQLException e){
}
return true;
}



public ResultSet executeQuery(String sql)
{
ResultSet rs;
try
{
if(con==null)
{
creatConnection();
}

Statement stmt=con.createStatement();

try{

rs=stmt.executeQuery(sql);
}catch(SQLException e){
return null;
}
}catch(SQLException e){
return null;
}
return rs;  
}


public void closeConnection()
{
if(con!=null)
{
try{
con.close();
}catch(SQLException e){
}finally
{
con=null;
}
}
}
}




.jsp文件如下:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page import="com.JDBConnection"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

</head>

<body>
<table width="382" height="245" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  <td height="36" align="center">将数据库中的内容添加到下拉列表中</td>
  </tr>
  <tr>
  <jsp:useBean id="connection" scope="request" class="com.JDBConnection"/>
  <td height="209" valign="top">

<%
String sql="Seclect *from tb_wyClass";
ResultSet rs=connection.executeQuery(sql);
%>


<form id="form1" name="form1" method="post" action="">
  <label>
  <select name="user">
<%
try{
while(rs.next())
{
%>
<option value="<%=rs.getString("name")%>"><%=rs.getString("name")%></option>
<%}}catch(Exception e){}
 
 
rs.close();
stmt.close();
con.close();
 
%>
  </select>
  </label>
  </form>
  </td>


  </tr>
</table>
</body>
</html>








出现如下错误:

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: /xialaliebiao.jsp(18,2) The value for the useBean class attribute com.JDBConnection is invalid.
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

org.apache.jasper.JasperException: /xialaliebiao.jsp(18,2) The value for the useBean class attribute com.JDBConnection is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1174)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3304)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.16 logs.


--------------------------------------------

Apache Tomcat/5.5.16

[解决办法]
其实这个应该发到JAVA WEB版
不过看到了就回你一下
com.JDBConnection必须要是一个JAVABEAN 或者POJO类
这里学的时候没认真吧
请遵守一下规则:
  1. 如果类的成员变量的名字是xxx,那么为了更改或获取成员变量的值,即更改或获取属性,在类中可以使用两个方法: 

  getXxx(),用来获取属性xxx。 

  setXxx(),用来修改属性xxx.。 

  2. 对于boolean类型的成员变量,即布尔逻辑类型的属性,允许使用"is"代替上面的"get"和"set"。 

  3. 类中方法的访问属性都必须是public的。 

  4. 类中如果有构造方法,那么这个构造方法也是public的并且是无参数的。


具体到你的com.JDBConnection就是没有getXxx和setXxx()。
如果用的Myeclipse可以右键->sourse->Generate Getters and Setters...,然后全沟上OK就行了。
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行