hi

i created a custom tag for database connections,sql... but doesn't work.

can any one help me to solve that

or

any one know how to create customtag for db means plz help me.
if u have coding for that plz send that.


codings:

1.tag handler program (db.java)

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.PageContext;


/**
 *
 * @author cepl
 */
public class db extends SimpleTagSupport implements DynamicAttributes {

    private ArrayList keys = new ArrayList(  );
    private ArrayList values = new ArrayList(  );
    private Connection conn;
     private Statement stmt;
     
   public void doTag() throws JspException, IOException{
        ResultSet rs;
        JspWriter out=getJspContext().getOut();
                try {
                 this.dbConnection(  );
                 rs = stmt.executeQuery("select c_name from emp");
                 while(rs.next()){
                  String name = rs.getString("c_name");
                  JspContext ctx = getJspContext(  );
                 ctx.setAttribute( "name", name );
                 }
                     }
            catch ( SQLException e ) {
        System.out.println( "SQL Error :" + e.toString(  ) );
      }
       finally {
        try {

          //Closing the statement and the connection objects
          this.closeConnection( conn, stmt );
        }
         catch ( Exception e ) {
          //Return error message on Exception
          System.out.println( "SQL Error :" + e.toString(  ) );
        }
      }
    }


     public void setDynamicAttribute( String uri, String localName, Object value )
                               throws JspException {
      keys.add( localName );
      values.add( value );

    }

    /**
     * This method uses the connection obtained from the registered datasource.
     * This datasource is registered in data-sources.xml file. This file can be
     * found under OC4J_HOME/j2ee/home/config directory. OC4J_HOME is where OC4J
     * is installed.
     */
    public void dbConnection(  ) {
      try {
        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/lemsdb_new;create=true;user=lems;password=kavitha"); 
        stmt = conn.createStatement(  );
         }
       catch ( SQLException se ) {
        System.out.println( "SQL Error while connecting to the database : " +
                              se.toString(  ) );
      }
       catch ( Exception e ) {
        System.out.println( "Other Error while connecting to the database  " +
                              e.toString(  ) );
      }
    }
    
    public void closeConnection( Connection conn, Statement stmt )
                           throws Exception {
      if ( conn != null ) {
        //Close the statement and connection
        stmt.close(  );
        conn.close(  );
      }

    }
}

2.tld file (tlds.tld)

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>ftld</short-name>
  <uri>/WEB-INF/tlds/ftld</uri>
  <!-- A validator verifies that the tags are used correctly at JSP
         translation time. Validator entries look like this: 
      <validator>
          <validator-class>com.mycompany.TagLibValidator</validator-class>
          <init-param>
             <param-name>parameter</param-name>
             <param-value>value</param-value>
	  </init-param>
      </validator>
   -->
  <!-- A tag library can register Servlet Context event listeners in
        case it needs to react to such events. Listener entries look
        like this: 
     <listener>
         <listener-class>com.mycompany.TagLibListener</listener-class> 
     </listener>
   -->
  <tag>
    <name>db</name>
    <tag-class>TagHan.db</tag-class>
    <body-content>scriptless</body-content>
    <variable>
      <name-given>name</name-given>
    </variable>
  </tag>
</taglib>

3.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib  uri="/WEB-INF/tlds/ftld" prefix="tlds"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <tlds:db></tlds:db>
</body>
</html>

or

any one know how to create customtag for db means plz help me.
if u have coding for that plz send that.

hi

can any one have the tag handler coding (java coding for custom tag) for database connection.

and coding for write script between the start and end tag.

eg:

<table>

<sql:connection conn="jdbc:derby://localhost:1527/sample;create=true;user=sam;password=aaa" 
sql="select name from emp" >

 <% while (result.next()) %>

<tr><td><b><%= result.getString("c_name")%></b></td></tr>
  
</sql:connection>

</table>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.