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

📄 empdb.java

📁 用java操作数据库
💻 JAVA
字号:
import wfc.app.*;
import wfc.core.*;
import wfc.ui.*;
import wfc.data.*;
import wfc.data.ui.*;
//EmpDB.java
public class EmpDB extends wfc.ui.Form {
    public void btnAdd_Click(Object sender, Event evt)     {
        try
        {
            m_rs.addNew();
            m_bAddNew = true;
            btnDelete.setText ("Cancel" );
            btnRefresh.setEnabled( false );
            btnAdd.setEnabled( false );
        }
        catch (Exception e)
        {
            handleADOException(e);
        }
    }
    public void btnDelete_Click(Object sender, Event evt)
    {
        try
        {
            if( m_bAddNew )
            {
                m_rs.cancelUpdate();
                m_bAddNew = false;
                btnDelete.setText ("Delete" );
                btnRefresh.setEnabled( true );
                btnAdd.setEnabled( true );
            }
            else
            {
                m_rs.delete(AdoEnums.Affect.CURRENT);
                m_rs.moveNext();
                if( m_rs.getEOF() )
                    m_rs.moveLast();
            }
        }
        catch (Exception e)
        {
            handleADOException(e);
        }
    }
    public void btnRefresh_Click(Object sender, Event evt)
    {
        try
        {
            m_rs.cancelBatch();
            m_rs.requery();
        }
        catch (Exception e)
        {
            handleADOException(e);
        }
    }
    public void btnUpdate_Click(Object sender, Event evt)
    {
        try
        {

            m_rs.update();
        }
        catch (Exception e)
        {
            handleADOException(e);
        }

        m_bAddNew = false;
        btnDelete.setText ("Delete" );
        btnRefresh.setEnabled( true );
        btnAdd.setEnabled( true );

    }
    public void btnClose_Click(Object sender, Event evt)
    {
        Application.exit();
    }

    Recordset m_rs;
    Connection m_con;
    DataBinder m_dataBinder;
    boolean    m_bAddNew;

    protected void finalize()
    {
        try
        {
            m_rs.close();
            m_con.close();
        }
        catch (Exception e)
        {
            handleADOException( e );
        }
    }

    public EmpDB()
    {
        //Required for Visual J++ Form Designer support
        initForm();

        //TODO: Add any constructor code after initForm call

        try
        {
            openDataConnection();
            initializeBindings();
        }
        catch (Exception e)
        {
            handleADOException( e );
        }
    }

    public void formClose(Event e)
    {
        Application.exit();
    }

    public static void main(String args[]) {
        Application.run( new EmpDB() );
    }

    void openDataConnection() {
        m_con = new Connection();
        m_rs = new Recordset();
        // The Jet provider connection was not used because your database contains one or more memo/ boolean fields.
        // The Beta2 release does not support binding to a memo/ boolean field using the Jet provider.
        // Here is the Jet connection string:  m_con.setConnectionString ("PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=C:\\Ph.D\\IDSc8440\\employee.mdb");
        m_con.setConnectionString ("Provider=MSDASQL;UID=admin;Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Ph.D\\IDSc8440\\employee.mdb");
        m_con.setCursorLocation (AdoEnums.CursorLocation.CLIENT);
        m_con.open();
        m_rs.setActiveConnection(m_con);
        m_rs.setSource("select * from Employee");
        m_rs.setCursorType(AdoEnums.CursorType.STATIC);
        m_rs.setCursorLocation(AdoEnums.CursorLocation.CLIENT);
        m_rs.setLockType(AdoEnums.LockType.OPTIMISTIC);
        m_rs.open();
        dataNavigator.setDataSource(m_rs);
    }

    void initializeBindings()
    {
        try
        {
            m_dataBinder = new DataBinder(m_rs);
            m_dataBinder.addBinding( this.editEmployeeID,"Text","EmployeeID"); 
            m_dataBinder.addBinding( this.editName,"Text","Name"); 
            m_dataBinder.addBinding( this.editSSN,"Text","SSN"); 
            m_dataBinder.addBinding( this.editSupervisor,"Text","Supervisor"); 
            m_dataBinder.addBinding( this.editDepartment,"Text","Department"); 
            m_dataBinder.addBinding( this.editPaytype,"Text","Paytype"); 
            m_dataBinder.addBinding( this.chkBenefits,"Checked","Benefits"); 
            m_dataBinder.addBinding( this.chkFlextime,"Checked","Flextime"); 
            m_dataBinder.addBinding( this.chkProbation,"Checked","Probation"); 
            m_rs.moveFirst();
        }
        catch (Exception e)
        {
            handleADOException( e );
        }
    }

    void handleADOException(Exception e)
    {
        e.printStackTrace();
        MessageBox.show( e.toString(), "employee" );
    }

    /**
    * NOTE: The following code is required by the Visual J++ form
    * designer.  It can be modified using the form editor.  Do not
    * modify it using the code editor.
    */

    Container components = new Container();
    Label labelEmployeeID = new Label();
    Edit editEmployeeID = new Edit();
    Label labelName = new Label();
    Edit editName = new Edit();
    Label labelSSN = new Label();
    Edit editSSN = new Edit();
    Label labelSupervisor = new Label();
    Edit editSupervisor = new Edit();
    Label labelDepartment = new Label();
    Edit editDepartment = new Edit();
    Label labelPaytype = new Label();
    Edit editPaytype = new Edit();
    Checkbox chkBenefits = new Checkbox();
    Checkbox chkFlextime = new Checkbox();
    Checkbox chkProbation = new Checkbox();
    Button btnAdd = new Button();
    Button btnDelete = new Button();
    Button btnRefresh = new Button();
    Button btnUpdate = new Button();
    Button btnClose = new Button();
    DataNavigator dataNavigator = new DataNavigator();

    private void initForm()
    {
        labelEmployeeID.setName ("labelEmployeeID" );
        labelEmployeeID.setBackColor (wfc.ui.Color.CONTROL);
        labelEmployeeID.setTabIndex ( 0 );
        labelEmployeeID.setText ("EmployeeID" );
        labelEmployeeID.setLocation (new Point ( 10, 20 )  );
        labelEmployeeID.setSize (new Point ( 100, 20 )  );
        editEmployeeID.setName ("editEmployeeID" );
        editEmployeeID.setBackColor (wfc.ui.Color.WINDOW);
        editEmployeeID.setTabIndex ( 1 );
        editEmployeeID.setText ("EmployeeID" );
        editEmployeeID.setLocation (new Point ( 120, 20 )  );
        editEmployeeID.setCursor (wfc.ui.Cursor.IBEAM);
        editEmployeeID.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editEmployeeID.setSize (new Point ( 280, 20 )  );
        labelName.setName ("labelName" );
        labelName.setBackColor (wfc.ui.Color.CONTROL);
        labelName.setTabIndex ( 2 );
        labelName.setText ("Name" );
        labelName.setLocation (new Point ( 10, 50 )  );
        labelName.setSize (new Point ( 100, 20 )  );
        editName.setName ("editName" );
        editName.setBackColor (wfc.ui.Color.WINDOW);
        editName.setTabIndex ( 3 );
        editName.setText ("Name" );
        editName.setLocation (new Point ( 120, 50 )  );
        editName.setCursor (wfc.ui.Cursor.IBEAM);
        editName.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editName.setSize (new Point ( 280, 20 )  );
        labelSSN.setName ("labelSSN" );
        labelSSN.setBackColor (wfc.ui.Color.CONTROL);
        labelSSN.setTabIndex ( 4 );
        labelSSN.setText ("SSN" );
        labelSSN.setLocation (new Point ( 10, 80 )  );
        labelSSN.setSize (new Point ( 100, 20 )  );
        editSSN.setName ("editSSN" );
        editSSN.setBackColor (wfc.ui.Color.WINDOW);
        editSSN.setTabIndex ( 5 );
        editSSN.setText ("SSN" );
        editSSN.setLocation (new Point ( 120, 80 )  );
        editSSN.setCursor (wfc.ui.Cursor.IBEAM);
        editSSN.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editSSN.setSize (new Point ( 280, 20 )  );
        labelSupervisor.setName ("labelSupervisor" );
        labelSupervisor.setBackColor (wfc.ui.Color.CONTROL);
        labelSupervisor.setTabIndex ( 6 );
        labelSupervisor.setText ("Supervisor" );
        labelSupervisor.setLocation (new Point ( 10, 110 )  );
        labelSupervisor.setSize (new Point ( 100, 20 )  );
        editSupervisor.setName ("editSupervisor" );
        editSupervisor.setBackColor (wfc.ui.Color.WINDOW);
        editSupervisor.setTabIndex ( 7 );
        editSupervisor.setText ("Supervisor" );
        editSupervisor.setLocation (new Point ( 120, 110 )  );
        editSupervisor.setCursor (wfc.ui.Cursor.IBEAM);
        editSupervisor.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editSupervisor.setSize (new Point ( 280, 20 )  );
        labelDepartment.setName ("labelDepartment" );
        labelDepartment.setBackColor (wfc.ui.Color.CONTROL);
        labelDepartment.setTabIndex ( 8 );
        labelDepartment.setText ("Department" );
        labelDepartment.setLocation (new Point ( 10, 140 )  );
        labelDepartment.setSize (new Point ( 100, 20 )  );
        editDepartment.setName ("editDepartment" );
        editDepartment.setBackColor (wfc.ui.Color.WINDOW);
        editDepartment.setTabIndex ( 9 );
        editDepartment.setText ("Department" );
        editDepartment.setLocation (new Point ( 120, 140 )  );
        editDepartment.setCursor (wfc.ui.Cursor.IBEAM);
        editDepartment.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editDepartment.setSize (new Point ( 280, 20 )  );
        labelPaytype.setName ("labelPaytype" );
        labelPaytype.setBackColor (wfc.ui.Color.CONTROL);
        labelPaytype.setTabIndex ( 10 );
        labelPaytype.setText ("Paytype" );
        labelPaytype.setLocation (new Point ( 10, 170 )  );
        labelPaytype.setSize (new Point ( 100, 20 )  );
        editPaytype.setName ("editPaytype" );
        editPaytype.setBackColor (wfc.ui.Color.WINDOW);
        editPaytype.setTabIndex ( 11 );
        editPaytype.setText ("Paytype" );
        editPaytype.setLocation (new Point ( 120, 170 )  );
        editPaytype.setCursor (wfc.ui.Cursor.IBEAM);
        editPaytype.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        editPaytype.setSize (new Point ( 280, 20 )  );
        chkBenefits.setName ("chkBenefits" );
        chkBenefits.setBackColor (wfc.ui.Color.CONTROL);
        chkBenefits.setTabIndex ( 12 );
        chkBenefits.setText ("Benefits" );
        chkBenefits.setLocation (new Point ( 10, 200 )  );
        chkBenefits.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        chkBenefits.setSize (new Point ( 120, 20 )  );
        chkBenefits.setAlignText(LeftRightAlignment.LEFT);
        chkFlextime.setName ("chkFlextime" );
        chkFlextime.setBackColor (wfc.ui.Color.CONTROL);
        chkFlextime.setTabIndex ( 13 );
        chkFlextime.setText ("Flextime" );
        chkFlextime.setLocation (new Point ( 10, 230 )  );
        chkFlextime.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        chkFlextime.setSize (new Point ( 120, 20 )  );
        chkFlextime.setAlignText(LeftRightAlignment.LEFT);
        chkProbation.setName ("chkProbation" );
        chkProbation.setBackColor (wfc.ui.Color.CONTROL);
        chkProbation.setTabIndex ( 14 );
        chkProbation.setText ("Probation" );
        chkProbation.setLocation (new Point ( 10, 260 )  );
        chkProbation.setAnchor(ControlAnchor.TOPLEFTRIGHT);
        chkProbation.setSize (new Point ( 120, 20 )  );
        chkProbation.setAlignText(LeftRightAlignment.LEFT);
        btnAdd.setAnchor(ControlAnchor.BOTTOMLEFT);
        btnAdd.setName ("btnAdd" );
        btnAdd.setLocation (new Point ( 12, 290 )  );
        btnAdd.setSize (new Point ( 70, 30 )  );
        btnAdd.setTabIndex ( 15 );
        btnAdd.setText ("&Add" );
        btnAdd.addOnClick (new EventHandler ( this.btnAdd_Click )  );
        btnDelete.setAnchor(ControlAnchor.BOTTOMLEFT);
        btnDelete.setName ("btnDelete" );
        btnDelete.setLocation (new Point ( 94, 290 )  );
        btnDelete.setSize (new Point ( 70, 30 )  );
        btnDelete.setTabIndex ( 15 );
        btnDelete.setText ("&Delete" );
        btnDelete.addOnClick (new EventHandler ( this.btnDelete_Click )  );
        btnRefresh.setAnchor(ControlAnchor.BOTTOMLEFT);
        btnRefresh.setName ("btnRefresh" );
        btnRefresh.setLocation (new Point ( 176, 290 )  );
        btnRefresh.setSize (new Point ( 70, 30 )  );
        btnRefresh.setTabIndex ( 15 );
        btnRefresh.setText ("&Refresh" );
        btnRefresh.addOnClick (new EventHandler ( this.btnRefresh_Click )  );
        btnUpdate.setAnchor(ControlAnchor.BOTTOMLEFT);
        btnUpdate.setName ("btnUpdate" );
        btnUpdate.setLocation (new Point ( 258, 290 )  );
        btnUpdate.setSize (new Point ( 70, 30 )  );
        btnUpdate.setTabIndex ( 15 );
        btnUpdate.setText ("&Update" );
        btnUpdate.addOnClick (new EventHandler ( this.btnUpdate_Click )  );
        btnClose.setAnchor(ControlAnchor.BOTTOMLEFT);
        btnClose.setName ("btnClose" );
        btnClose.setLocation (new Point ( 340, 290 )  );
        btnClose.setSize (new Point ( 70, 30 )  );
        btnClose.setTabIndex ( 15 );
        btnClose.setText ("&Close" );
        btnClose.addOnClick (new EventHandler ( this.btnClose_Click )  );
        dataNavigator.setName ("dataNavigator" );
        dataNavigator.setTabIndex ( 15 );
        dataNavigator.setText ("" );
        dataNavigator.setDataMember (null );
        dataNavigator.setAnchor(ControlAnchor.BOTTOMLEFTRIGHT);
        dataNavigator.setLocation (new Point ( 10, 330 )  );
        dataNavigator.setSize (new Point ( 400, 20 )  );
        this.setBackColor (wfc.ui.Color.CONTROL );
        this.setLocation (new Point ( 7, 7 )  );
        this.setSize (new Point ( 420, 390)  );
        this.setText ("employee" );
        this.setNewControls (new Control[] {
            dataNavigator,
            btnAdd,
            btnDelete,
            btnRefresh,
            btnUpdate,
            btnClose,
            labelEmployeeID,
            editEmployeeID,
            labelName,
            editName,
            labelSSN,
            editSSN,
            labelSupervisor,
            editSupervisor,
            labelDepartment,
            editDepartment,
            labelPaytype,
            editPaytype,
            chkBenefits,
            chkFlextime,
            chkProbation}  );
    }
    //NOTE: End of form designer support code.

    public static class ClassInfo extends Form.ClassInfo
    {
        //TODO: Add your property and event infos here
    }
}

⌨️ 快捷键说明

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