home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / samples.z / IJDBC_LookUp.wxc < prev    next >
Text File  |  1996-12-10  |  6KB  |  219 lines

  1. Save Format v1.3
  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 119;
  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 122;
  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.writeln( "<CENTER><H1>JDBC ISAPI LookUp Sample</CENTER></H1>" );
  129.             server.writeln( "<P>\n<BR>\n<HR>\n<BR>" );           
  130.             
  131.             // Trying to load the Sybase JDBC Driver
  132.             Class.forName( "com.sybase.jdbc.SybDriver" );
  133.                         
  134.             // Creating the URL from the 'host' and 'port' specified
  135.             String urlBase  = "jdbc:sybase:Tds:";
  136.             String host     = null;
  137.             String port     = null;
  138.             String userId   = "dba";
  139.             String password = "sql";            
  140.             String query    = "SELECT * FROM DBA.customer WHERE customer.id = 101";
  141.                     
  142.             if( server.getIsPostMethod() ) {
  143.                 host = server.getFormVariable( "host" );
  144.                 port = server.getFormVariable( "port" );
  145.             } else {
  146.                 host = server.getQueryVariable( "host" );
  147.                 port = server.getQueryVariable( "port" );
  148.             }
  149.             
  150.             String url = urlBase + host + ":" + port;
  151.             server.write( "<P>Trying to connect to: " );
  152.             server.writeln( url );
  153.             
  154.             // Connect to the database at that URL.
  155.             Connection _conn = DriverManager.getConnection( url, userId, password );           
  156.             Statement _stmt = _conn.createStatement();                    
  157.  
  158.             // Execute the query
  159.             server.write( "<P>Sending query: " );
  160.             server.write( query );
  161.             server.writeln( "<BR>" );
  162.             ResultSet _rs = _stmt.executeQuery( query );
  163.  
  164.             // Print out the customer's info
  165.             _rs.next();
  166.             server.writeln( "<H2><em>Customer Information</em></H2>" );
  167.             
  168.             
  169.             server.write( "<P><strong>Id</strong>: " );
  170.             server.writeln( _rs.getString( 1 ) );
  171.             server.write( "<P><strong>First Name</strong>: ");
  172.             server.writeln( _rs.getString( 2 ) );
  173.             server.write( "<P><strong>Last Name</strong>: " );
  174.             server.writeln( _rs.getString( 3 ) );
  175.             server.write( "<P><strong>Adress</strong>: " );
  176.             server.writeln( _rs.getString( 4 ) );
  177.             server.write( "<P><strong>City</strong>: " );
  178.             server.writeln( _rs.getString( 5 ) );
  179.             server.write( "<P><strong>State</strong>: " );
  180.             server.writeln( _rs.getString( 6 ) );
  181.             server.write( "<P><strong>Zip</strong>: " );
  182.             server.writeln( _rs.getString( 7 ) );
  183.             server.write( "<P><strong>Phone</strong>: " );
  184.             server.writeln( _rs.getString( 8 ) );
  185.             server.write( "<P><strong>Company Name</strong>: " );
  186.             server.writeln( _rs.getString( 9 ) );
  187.                        
  188.             // Closing the statement and the connection
  189.             if( _stmt != null ) {
  190.                 _stmt.close();
  191.             }
  192.  
  193.             if( _conn != null ) {
  194.                 _conn.close();
  195.             }
  196.                         
  197.               server.writeln( "<P>\n<BR>\n<HR>\n<BR>" );      
  198.             
  199.         } catch( WebServiceException e ) {
  200.             server.writeln( "<H2>WebService Exception: " + e.getMessage() );
  201.         } catch( ClassNotFoundException e ) {
  202.             server.writeln( "<H2>ClassNotFound Exception: " + e.getMessage() );
  203.         } catch( Exception e ) {
  204.             server.writeln( "<H2>Unexpected Exception: " + e.getMessage() );        
  205.         }        
  206.         
  207.     }
  208.  
  209. @end-code;
  210.  
  211. @begin-code Code "IJDBC_LookUp::EndApp(String args[])"
  212.  
  213.     public void EndApp(String args[])
  214.     {
  215.     }
  216.  
  217. @end-code;
  218. @end;
  219.