RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
java(j2ee)应用-简单自定义标签开发全过程----之一
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 11:01
  • 来源:未知

标签应用:数据源MyDataSource2用户sa密码(空)数据库pubs 表:employee

说明:windows ODBC ---SQL  数据库pubs 表:employee

功能:检索job_lvl>=100的数据记录,在JSP页面中任意想调用的地方调用它。

1:标签处理程序:EmployeeTag.java

package net.inber;

import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;

import java.util.Hashtable;import java.io.Writer;import java.io.IOException;import java.util.Date;      

import java.sql.*;import java.math.*;import java.util.*;

public class EmployeeTag extends TagSupport{        Connection con = null;  Statement state = null;  ResultSet rs = null;          public EmployeeTag()throws ClassNotFoundException  {    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  }         public int doStartTag() throws JspTagException {      return EVAL_BODY_INCLUDE;    }     public int doEndTag()throws JspTagException {  String dateString =new Date().toString();  try  {   pageContext.getOut().write("");       try{      JspWriter out = pageContext.getOut();      con = DriverManager.getConnection("jdbc:odbc:MyDataSource2","sa","");      String strQuery = "select emp_id,fname,lname,job_lvl,hire_date from employee where

job_lvl>=100";      state = con.createStatement();      rs = state.executeQuery(strQuery);      String emp_id=null;      String fname=null;      String lname=null;      String job_lvl=null;      String hire_date=null;            out.write("<table width='677' height='22' border='1'>") ;      while(rs.next())      { emp_id = rs.getString(1);    fname = rs.getString(2); lname = rs.getString(3);    job_lvl = rs.getString(4); hire_date = rs.getString(5); out.write("<tr>");     out.write("<td width='25%'>"+emp_id+"</td>"); out.write("<td width='17%'>"+fname+"</td>"); out.write("<td width='13%'>"+lname+"</td>"); out.write("<td width='9%'>"+job_lvl+"</td>"); out.write("<td width='36%'>"+hire_date+"</td>");  out.write("</tr>");       }out.write("</table>");                  

    }    catch(Exception e)    {   //throw new JspTagException("Fatal error:connect error!");     }        }  catch(IOException ex)  {   throw new JspTagException("Fatal error:hello tag conld not write to

JSP out");  }  return EVAL_PAGE; // return SKIP_PAGE; }}2.TLD文件EmployeeTag.tld<?xml version="1.0" encoding="ISO-8859-1" ?>