Try this one
Eric

> -----Message d'origine-----
> De: A mailing list for discussion about Sun Microsystem's Java Servlet
> API Technology. [mailto:[EMAIL PROTECTED]]De la part de
> Leung Kwok Fai
> Date: mercredi 17 mars 1999 15:44
> �: [EMAIL PROTECTED]
> Objet: Servlet to access SQL Server
>
>
>         I'm going to implement to access the Microsoft SQL Server
> through the
> servlet. I will set up a SQL Server on a NT Server and the middle-tier is
> located in JServ. Is anyone has some source code or some advise
> for me. I'm
> still a new user for Java Servlet.
>
> Thank you!
>
> __________________________________________________________________
> _________
> To unsubscribe, send email to [EMAIL PROTECTED] and include
> in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
import java.sql.*;

/**
 * Show others how to write Java, JDBC/ODBC bridge applications
 *
 * Run this from a command line as (make adjustments for path seperator):
 *       jre -cp .\ JDBCExampleSQLServer 1 smb1
 *
 * @author Stephen M. Barrett
 * @since 1998.08.11
 */
public class JDBCExampleSQLServer {

   String gPersonIdent = null;

   /* MicroSoft insists on doing everything differently.
      We need to test against DBC connect messages.
   */
   String MSODBC1 = "[SQL Server]Changed language setting to '",
          MSODBC2 = "[SQL Server]Changed database context to '",
          MSODBCTester = null;

   /**
    * Constructor
    */
   public JDBCExampleSQLServer(){ super(); }

   static public void main(String args[]){

      (new JDBCExampleSQLServer()).GrabDBInfo();
   }// main

   /**
    * Connect to DB and process request specified by command line args
    */
   void GrabDBInfo( ){

      Driver d = null;
      Connection c = null;
      Statement s = null;
      ResultSet r = null;
      ResultSetMetaData m = null;
      SQLWarning w = null;

      // Get the required class for the bridge
      try{
       d = (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      }catch( Exception e ){ e.printStackTrace(); }

      // Establish a connection
      try{
       c = DriverManager.getConnection("jdbc:odbc:StocksDSN","Administrateur", "kakou" );
      }catch( SQLException e ){ e.printStackTrace(); }

      // Deal with any warnings
      try{
       w = c.getWarnings();
       while( w != null ){
          MSODBCTester = ((Object)w).toString();

          // Here we deal with SQL Server specific returns (only need 4 MS)
          if( MSODBCTester.indexOf( MSODBC1 ) == -1 &&
              MSODBCTester.indexOf( MSODBC2 ) == -1 )
            System.out.println( "Connect Error: " + w );

        w = w.getNextWarning();
       }

       c.clearWarnings();
      }catch( SQLException e ){ e.printStackTrace(); }

      // Create a new statment object so that we can issue SQL
      try{
       s = c.createStatement();
      }catch( SQLException e ){ e.printStackTrace(); }

                        try{
        r = s.executeQuery( "select * from Produits" );
      }catch( SQLException e ){ e.printStackTrace(); }

         try{
          if( r.next() ){  // returns true if there are any result rows

            m = r.getMetaData();  // Very useful information
            System.out.println("\n nombre de kakous = " + Integer.toString(m.getColumnCount()));

            // Step through the meta data and print out the column names
            for( int I = 1; I <= (m.getColumnCount()); I++ ) {
              System.out.println( m.getColumnName( I ));
            }// for
           }// if
          }catch( SQLException e ){ e.printStackTrace(); }

      }
}//JDBCSample, that's all folks!



Reply via email to