User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 391,705 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,227 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 428 | Replies: 1
Reply
Join Date: May 2008
Posts: 14
Reputation: luxmi_gee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
luxmi_gee luxmi_gee is offline Offline
Newbie Poster

customtag for database connection

  #1  
Jun 2nd, 2008
hi

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

here i paste the which i tried,

db.java (tag handler)
---------
  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import javax.servlet.jsp.tagext.DynamicAttributes;
  9. import javax.servlet.jsp.tagext.*;
  10. import javax.servlet.jsp.JspWriter;
  11. import javax.servlet.jsp.JspException;
  12. import javax.servlet.jsp.JspContext;
  13. import javax.servlet.jsp.PageContext;
  14.  
  15.  
  16. /**
  17.  *
  18.  * @author cepl
  19.  */
  20. public class db extends SimpleTagSupport implements DynamicAttributes {
  21.  
  22. private ArrayList keys = new ArrayList( );
  23. private ArrayList values = new ArrayList( );
  24. private Connection conn;
  25. private Statement stmt;
  26. ResultSet rs;
  27.  
  28. public void doTag() throws JspException, IOException{
  29.  
  30. JspWriter out=getJspContext().getOut();
  31. try {
  32.  
  33. conn = DriverManager.getConnection("jdbc:derby://localhost:1527/lemsdb_new;create=true;user=lems;password=kavitha");
  34. stmt = conn.createStatement( );
  35. rs = stmt.executeQuery("select c_name from lems.conlist");
  36. while(rs.next()){
  37. String name = rs.getString("c_name");
  38. JspContext ctx = getJspContext( );
  39. ctx.setAttribute( "name", name );
  40. }
  41. }
  42. catch ( SQLException e ) {
  43. System.out.println( "SQL Error :" + e.toString( ) );
  44. }
  45. finally {
  46. try {
  47.  
  48. //Closing the statement and the connection objects
  49. this.closeConnection( conn, stmt );
  50. }
  51. catch ( Exception e ) {
  52. //Return error message on Exception
  53. System.out.println( "SQL Error :" + e.toString( ) );
  54. }
  55. }
  56. }
  57.  
  58.  
  59. public void setDynamicAttribute( String uri, String localName, Object value )
  60. throws JspException {
  61. keys.add( localName );
  62. values.add( value );
  63.  
  64. }
  65.  
  66. public void closeConnection( Connection conn, Statement stmt )
  67. throws Exception {
  68. if ( conn != null ) {
  69. //Close the statement and connection
  70. stmt.close( );
  71. conn.close( );
  72. }
  73.  
  74. }
  75. }

but it doesn't work

thanks
Last edited by peter_budo : Jun 2nd, 2008 at 6:48 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: customtag for database connection

  #2  
Jun 2nd, 2008
no, database connections don't belong in JSP.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JSP Marketplace
Thread Tools Display Modes

Other Threads in the JSP Forum

All times are GMT -4. The time now is 2:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC