hi

can any one help me to create customtag for database connection.

here i paste the which i tried,

db.java (tag handler)
---------

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;
     ResultSet rs;
     
   public void doTag() throws JspException, IOException{
        
        JspWriter out=getJspContext().getOut();
                try {
                    
                conn = DriverManager.getConnection("jdbc:derby://localhost:1527/lemsdb_new;create=true;user=lems;password=kavitha"); 
        stmt = conn.createStatement(  );
                 rs = stmt.executeQuery("select c_name from lems.conlist");
                 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 );

    }

      public void closeConnection( Connection conn, Statement stmt )
                           throws Exception {
      if ( conn != null ) {
        //Close the statement and connection
        stmt.close(  );
        conn.close(  );
      }

    }
}

but it doesn't work

thanks

no, database connections don't belong in JSP.

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.