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

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