⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adocore.java

📁 vc ADO 连接数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ADOCORE:  Implementation functions for demonstrating ADO via 
//           Visual J++ and Java Type Library Wizard
//
//           Unlike other samples for the ADO Rosetta Stone,
//           for this code to work you need to create a ODBC 
//           data source named AdoDemo which points to the
//           ADODEMO.MDB file.
//
//           Non-ADO Native and Native ADO Error Handling is embedded
//           within each exception handling function.  
//           AdoUtils.AdoErrorEx handles processing errors raised to 
//           the Ado Errors Collection

import com.ms.com.*;
import java.awt.*;
import msado15.*;

class ADOCore 
{
    ADOUtils    Utl1;

    // Need two Distinct "empty" VARIANTs and BSTR for various Methods
    public  Variant     vtEmpty;
    public  Variant     vtEmpty2;
    public  String      bstrEmpty;          // Matt:  Is there a Java type for BSTR?

    // Connection and SQL Strings
    public  String      bstrAccessConnect;
    public  String      bstrOpenAccess;
    public  String      bstrOpenAccessWithParam;
    public  String      bstrSQLCreate;
    public  String      bstrSQLDrop;
    public  String      bstrStoredProc;

    public void init()
    {
        // Initalize instance of helper classes
        Utl1 = new ADOUtils();
        Utl1.init();

        vtEmpty   = new Variant();
        vtEmpty2  = new Variant();
        bstrEmpty = new String();

        vtEmpty.noParam();
        vtEmpty2.noParam();

        // Initialize connection variables
        bstrAccessConnect        = new String( "DSN=AdoDemo;UID=admin;PWD=;" );
        bstrOpenAccess           = new String( "SELECT * FROM Authors" );
        bstrOpenAccessWithParam  = new String( "SELECT * FROM Authors WHERE Au_ID < ?" );
        bstrSQLCreate            = new String( "create proc sp_AdoTest( @InParam int, @OutParam int OUTPUT ) as select @OutParam = @InParam + 10SELECT * FROM Authors WHERE State <> 'CA' return @OutParam +10" );
        bstrSQLDrop              = new String( "if exists (select * from sysobjects where id = object_id('dbo.sp_AdoTest') and sysstat & 0xf = 4) drop procedure dbo.sp_AdoTest" );
        bstrStoredProc           = new String( "sp_Adotest" );

        // Initialize "Empty" varaibles
        vtEmpty.VariantClear();
        vtEmpty2.VariantClear();
        bstrEmpty = new String("");

    }

    public void destroy()
    {
        // Destroy instance of helper classes
        if ( Utl1 != null ) Utl1 = null;

        // Destroy empty variables
        vtEmpty.VariantClear();
        vtEmpty2.VariantClear();
    }



//-----------------------
// Dialog Event Handlers
//-----------------------

    public void OpenAccessDatabase( List List1 )
    {   
        msado15._Connection  Conn1   = new msado15.Connection();
        msado15._Command     Cmd1    = null;
        msado15._Recordset   Rs1     = new msado15.Recordset();
        boolean              bEOF;

        Variant              v1      = new Variant();
        Variant              v2      = new Variant();

        // Trap any error/exception
        try
        {
            //------------------------
            // Open Connection Object
            //------------------------

            // Warm & Fuzzy for user
            List1.clear();
            List1.addItem( "Opening Access Database ADODEMO.MDB..." );
            List1.addItem( "    ...Assumes ADODEMO.MDB is in the same directory" );
            List1.addItem( "    ...With Error Handling Using Connection Object"  );

            // Create Connection Object (1.5 Version)
            Conn1.putConnectionString( bstrAccessConnect );
            Conn1.Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );

            //-----------------------------------
            // Open Parameterized Command Object
            //-----------------------------------
            List1.addItem( "    ...Parameterized Command Object" );

            // Create Command Object
            Cmd1= new msado15.Command();
            Cmd1.putActiveConnection( Conn1 );
            Cmd1.putCommandText     ( bstrOpenAccessWithParam );
//            Cmd1.putCommandText     ( "SELECT * FROM Authors WHERE Au_ID < ?" );

            // Create Parameter Object
            v1.putString( "P1" );
            v2.putInt( 5 );
            Cmd1.getParameters().Append(  Cmd1.CreateParameter( v1.getString(),
                                                                msado15.DataTypeEnum.adInteger,
                                                                msado15.ParameterDirectionEnum.adParamInput,
                                                                0,
                                                                v2 ) );

            // Open Recordset Object
            Rs1 = Cmd1.Execute( vtEmpty, vtEmpty2, msado15.CommandTypeEnum.adCmdText );

            // Hmm... Why is this necessary
            Rs1.Requery( -1 );

            //----------------------------------------
            // Manipulate Recordset/Fields Collection
            //----------------------------------------

            List1.addItem( "    ...Forward-Only Recordset" );
            List1.addItem( "Dumping contents of each record..." );

            // While not on EOF...
            bEOF = Rs1.getEOF();
            if ( bEOF == false )
            {
                v1.putInt( 0 );
                v2.putInt( 1 );

                while ( bEOF == false )
                {
                    // Display value in each field
                    List1.addItem( "     " + 
                                   Rs1.getFields().getItem( v1 ).getValue()  +
                                   " | " +
                                   Rs1.getFields().getItem( v2 ).getValue() );

                    // Move to next record
                    Rs1.MoveNext();
                    bEOF = Rs1.getEOF();
                }
            }

            // Warm & Fuzzy for user
            List1.addItem( "Dumping properties of each column in the recordset..." );

            // Reset recordset (1.5 Version)
            Rs1.Requery( -1 );

            // Iterate through columns
            for ( int i=0; i < Rs1.getFields().getCount(); i++ )
            {
                v1.putInt( (int) i );

                // Display Field #
                List1.addItem("    Field #" + (i + 1) );

                // Display Actual Size
                List1.addItem( "        ...Actual Size  = " + Rs1.getFields().getItem( v1 ).getActualSize() ); 

                // Display Attributes
                List1.addItem( "        ...Attributes   = " + Rs1.getFields().getItem( v1 ).getAttributes() ); 

                // Display Defined Size
                List1.addItem( "        ...Defined Size = " + Rs1.getFields().getItem( v1 ).getDefinedSize() ); 

                // Display Name
                List1.addItem( "        ...Name         = " + Rs1.getFields().getItem( v1 ).getName() );

                // Display Type
                List1.addItem( "        ...Type         = " + Utl1.GetType( Rs1.getFields().getItem( v1 ).getType() ) );

                // Display Value
                if( Rs1.getFields().getItem( v1 ).getType() == msado15.DataTypeEnum.adGUID )
                {
                    List1.addItem( "        ...Value      = <GUID>" );
                }
                else
                {
                    List1.addItem  ( "        ...Value      = " + Rs1.getFields().getItem( v1 ).getValue() );
                }
            }

            // Successful Shutdown
            List1.addItem( "*** Success! ***" );
        }
        // Catch Blocks
        catch (com.ms.com.ComFailException e)    
        {
            Utl1.LogException( e, List1, Conn1 );
        }
        catch(Exception e) 
        {
            Utl1.LogException( e, List1, Conn1 );
        }

        //----------------------------------
        // Miscellaneous (graceful) Cleanup
        // with quiet error trapping
        //----------------------------------

        if ( Rs1      != null ) Rs1     = null;
        if ( Cmd1     != null ) Cmd1    = null;
        if ( Conn1    != null ) Conn1   = null;
    }

    public void ProviderProperties( List List1 )
    {  
        msado15._Connection  Conn1  = new msado15.Connection();
        msado15._Command     Cmd1   = null;
        msado15._Recordset   Rs1    = new msado15.Recordset();

        int             i = 0;        
        Variant         varTemp = new Variant();

        // Trap any error/exception
        try
        {
            //-------------------
            // Connection Object
            //-------------------

            // Warm & Fuzzy for user
            List1.clear();
        
            // Create Connection Object (1.5 Version)
            Conn1.putConnectionString ( bstrAccessConnect );
            Conn1.Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );

            // Dump connection properties
            List1.addItem( "*** *** *** *** *** Dumping contents of the Properties Collection for the Connection object *** *** *** *** ***" );
            Utl1.DumpProperty( List1, Conn1, Conn1.getProperties() );

            //----------------
            // Command Object
            //----------------

            // Create Command Object
            Cmd1= new msado15.Command();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -