home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / samples.z / IJDBC_LookUp.wxc < prev    next >
Text File  |  1997-01-28  |  7KB  |  236 lines

  1. Save Format v2.0(1)
  2. @begin ClassFile "IJDBC_LookUp"
  3.  Exported 0;
  4.  Abstract 0;
  5.  Interface 0;
  6.  PackageName "";
  7.  Language "Java";
  8.  
  9.  @begin UserFunction "IJDBC_LookUp()"
  10.   Compiler 1;
  11.   GencodeSrcLine 12;
  12.   FunctionName "IJDBC_LookUp::IJDBC_LookUp()";
  13.  @end;
  14.  
  15.  @begin UserFunction "main(String args[])"
  16.   Compiler 1;
  17.   GencodeFunction 1;
  18.   GencodeSrcLine 16;
  19.   FunctionName "IJDBC_LookUp::main(String args[])";
  20.  @end;
  21.  
  22.  @begin UserFunction "CreateMainForm()"
  23.   Compiler 1;
  24.   GencodeFunction 1;
  25.   GencodeSrcLine 23;
  26.   FunctionName "IJDBC_LookUp::CreateMainForm()";
  27.  @end;
  28.  
  29.  @begin UserFunction "StartApp(String args[])"
  30.   Compiler 1;
  31.   GencodeSrcLine 26;
  32.   FunctionName "IJDBC_LookUp::StartApp(String args[])";
  33.  @end;
  34.  
  35.  @begin UserFunction "RunApp(String args[])"
  36.   Compiler 1;
  37.   GencodeSrcLine 34;
  38.   FunctionName "IJDBC_LookUp::RunApp(String args[])";
  39.  @end;
  40.  
  41.  @begin UserFunction "EndApp(String args[])"
  42.   Compiler 1;
  43.   GencodeSrcLine 136;
  44.   FunctionName "IJDBC_LookUp::EndApp(String args[])";
  45.  @end;
  46.  
  47.  @begin HPPPrefixBlock
  48. @begin-code HPPPrefix
  49.  
  50. // add your custom import statements here
  51. import java.io.*;
  52. import java.sql.*;
  53.  
  54. @end-code;
  55.   GencodeSrcLine 6;
  56.  @end;
  57.  
  58.  @begin ClassContentsBlock
  59. @begin-code ClassContents
  60.  
  61.     // add your data members here
  62.     WebConnection server;
  63.  
  64. @end-code;
  65.   GencodeSrcLine 139;
  66.  @end;
  67.  
  68. @begin-code BaseClassList
  69.  
  70. extends Object
  71.  
  72. @end-code;
  73.  
  74. @begin-code GeneratedClassContents
  75.  
  76.  
  77. @end-code;
  78.  
  79. @begin-code Code "IJDBC_LookUp::IJDBC_LookUp()"
  80.  
  81.     public @CLASSNAME@()
  82.     {
  83.         super();
  84.     }
  85.  
  86. @end-code;
  87.  
  88. @begin-code Code "IJDBC_LookUp::main(String args[])"
  89.  
  90.     public static void main(String args[])
  91.     {
  92.         @@CLASSNAME@ app = new @CLASSNAME@();
  93.         app.StartApp(args);
  94.         app.RunApp(args);
  95.         app.EndApp(args);
  96.     }
  97.  
  98. @end-code;
  99.  
  100. @begin-code Code "IJDBC_LookUp::CreateMainForm()"
  101.  
  102.     public void CreateMainForm()
  103.     {
  104.     }
  105.  
  106. @end-code;
  107.  
  108. @begin-code Code "IJDBC_LookUp::StartApp(String args[])"
  109.  
  110.     public void StartApp(String args[])
  111.     {
  112.         server = new WebConnection(args);
  113.         if (! server.getConnected() ) {
  114.             System.err.println("Connection to Server failed");
  115.             System.exit(2);
  116.         }       
  117.     }
  118.  
  119. @end-code;
  120.  
  121. @begin-code Code "IJDBC_LookUp::RunApp(String args[])"
  122.  
  123.     public void RunApp(String args[])
  124.     {
  125.         //CreateMainForm();
  126.         try {
  127.             // Printing out headers
  128.             server.setResponseHeader( "Content-Type", "text/html" );    
  129.             server.writeln( "<CENTER><H1>JDBC ISAPI / NSAPI LookUp Sample</CENTER></H1>" );
  130.             server.writeln( "\n<BR>\n<HR>\n<BR>" );           
  131.  
  132.             // Trying to load the Sybase JDBC Driver
  133.             Class.forName( "com.sybase.jdbc.SybDriver" );
  134.  
  135.             // Creating the URL from the 'host' and 'port' specified
  136.             String urlBase   = "jdbc:sybase:Tds:";
  137.             String host      = null;
  138.             String port      = null;
  139.             String firstName = null;
  140.             String lastName  = null;
  141.             String userId    = "dba";
  142.             String password  = "sql";            
  143.  
  144.             if( server.getIsPostMethod() ) {
  145.                 host = server.getFormVariable( "host" );
  146.                 port = server.getFormVariable( "port" );
  147.                 firstName = server.getFormVariable( "firstName" );
  148.                 lastName  = server.getFormVariable( "lastName" );
  149.             } else {
  150.                 host = server.getQueryVariable( "host" );
  151.                 port = server.getQueryVariable( "port" );
  152.                 firstName = server.getQueryVariable( "firstName" );
  153.                 lastName  = server.getQueryVariable( "lastName" );
  154.             }
  155.  
  156.             String query = "SELECT * FROM DBA.customer WHERE";
  157.             if( firstName != null ) {
  158.                 query = query + " customer.fname = \'" + firstName +"\'";
  159.                 if( lastName != null ) {
  160.                     query = query + " AND customer.lname = " + "\'" + lastName + "\'";
  161.                 }
  162.             } else {
  163.                 query = query + " customer.lname = " +  "\'" + lastName + "\'";
  164.  
  165.             }
  166.  
  167.             String url = urlBase + host + ":" + port;
  168.             server.write( "<P>Trying to connect to: " );
  169.             server.writeln( url );
  170.  
  171.             // Connect to the database at that URL.
  172.             Connection _conn = DriverManager.getConnection( url, userId, password );           
  173.             Statement _stmt = _conn.createStatement();                    
  174.  
  175.             // Execute the query
  176.             server.write( "<P>Sending query: " );
  177.             server.write( query );
  178.             server.writeln( "<BR>" );
  179.             ResultSet _rs = _stmt.executeQuery( query );
  180.  
  181.             // Print out the customer's info
  182.             _rs.next();
  183.             server.writeln( "<H2><em>Customer Information</em></H2>" );
  184.  
  185.             server.writeln("<Normal>");
  186.             server.write( "<P><strong>First Name</strong>: ");
  187.             server.writeln( _rs.getString( 2 ) );
  188.             server.write( "<P><strong>Last Name</strong>: " );
  189.             server.writeln( _rs.getString( 3 ) );
  190.             server.write( "<P><strong>Adress</strong>: " );
  191.             server.writeln( _rs.getString( 4 ) );
  192.             server.write( "<P><strong>City</strong>: " );
  193.             server.writeln( _rs.getString( 5 ) );
  194.             server.write( "<P><strong>State</strong>: " );
  195.             server.writeln( _rs.getString( 6 ) );
  196.             server.write( "<P><strong>Zip</strong>: " );
  197.             server.writeln( _rs.getString( 7 ) );
  198.             server.write( "<P><strong>Phone</strong>: " );
  199.             server.writeln( _rs.getString( 8 ) );
  200.             server.write( "<P><strong>Company Name</strong>: " );
  201.             server.writeln( _rs.getString( 9 ) );
  202.             server.write( "<P><strong>Id</strong>: " );
  203.             server.writeln( _rs.getString( 1 ) );
  204.             server.writeln("</Normal>");
  205.  
  206.             // Closing the statement and the connection
  207.             if( _stmt != null ) {
  208.                 _stmt.close();
  209.             }
  210.  
  211.             if( _conn != null ) {
  212.                 _conn.close();
  213.             }
  214.  
  215.               server.writeln( "<P>\n<BR>\n<HR>\n<BR>" );                  
  216.  
  217.         } catch( WebServiceException e ) {
  218.             server.writeln( "<H2>WebService Exception: " + e.getMessage() );
  219.         } catch( ClassNotFoundException e ) {
  220.             server.writeln( "<H2>ClassNotFound Exception: " + e.getMessage() );
  221.         } catch( Exception e ) {
  222.             server.writeln( "<H2>Unexpected Exception: " + e.getMessage() );        
  223.         }        
  224.     }
  225.  
  226. @end-code;
  227.  
  228. @begin-code Code "IJDBC_LookUp::EndApp(String args[])"
  229.  
  230.     public void EndApp(String args[])
  231.     {
  232.     }
  233.  
  234. @end-code;
  235. @end;
  236.