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

📄 mainframe.java

📁 java DataBase Exp
💻 JAVA
字号:
/*源代码清单11-4*/

package database;

import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import java.sql.*;
import java.util.*;
import borland.jbcl.model.*;

public class MainFrame extends DecoratedFrame
{
  GridControl gridControl1 = new GridControl();
  Button button1 = new Button();
  Button button2 = new Button();
  Button button3 = new Button();
  Button button4 = new Button();
  PaneLayout paneLayout1 = new PaneLayout();
  Connection  connection=null;
  PreparedStatement statement=null;
  int row=0;
  int column=0;
  Vector data=new Vector();
  
  public MainFrame()
  {
    try {
      jbInit();
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      connection=DriverManager.getConnection("jdbc:odbc:temp");
      setGridControl();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    this.setTitle("数据库实例");
    gridControl1.setColumnCaptions(new String[] {"姓名", "性别", "年龄", "电话", "备注"});
    button1.setLabel("增加");
    button1.addActionListener(new MainFrame_button1_actionAdapter(this));
    button2.setLabel("修改");
    button2.addActionListener(new MainFrame_button2_actionAdapter(this));
    button3.setLabel("删除");
    button3.addActionListener(new MainFrame_button3_actionAdapter(this));
    button4.setLabel("退出");
    button4.addActionListener(new MainFrame_button4_actionAdapter(this));
    this.setLayout(paneLayout1);
    this.add(gridControl1, new PaneConstraints("gridControl1", "gridControl1", PaneConstraints.ROOT, 0.5f));
    this.add(button1, new PaneConstraints("button1", "gridControl1", PaneConstraints.BOTTOM, 0.11522633f));
    this.add(button2, new PaneConstraints("button2", "button1", PaneConstraints.RIGHT, 0.7428571f));
    this.add(button3, new PaneConstraints("button3", "button2", PaneConstraints.RIGHT, 0.6818182f));
    this.add(button4, new PaneConstraints("button4", "button3", PaneConstraints.RIGHT, 0.5589744f));
    gridControl1.setReadOnly(true);
    gridControl1.addSubfocusListener(new MainFrame_gridControl1_subfocusAdapter(this));
  }

  public static void main(String[] args)
  {
    MainFrame frame=new MainFrame();
    frame.setLocation(150,150);
    frame.setSize(550,400);
    frame.show();
  }

  void setGridControl()
  {
    try
    {
      data.removeAllElements();
      String sql="select * from people";
      statement=connection.prepareStatement(sql);
      ResultSet result=statement.executeQuery();
      while(result.next())
      {
        String name=result.getString("name");
        String sex=result.getString("sex");
        int age=result.getInt("age");
        String phone=result.getString("phone");
        String other=result.getString("other");
        data.addElement(name);
        data.addElement(sex);
        data.addElement(Integer.toString(age));
        data.addElement(phone);
        data.addElement(other);
      }
      result.close();
      statement.close();
      int len=data.size()/5;
      String[][] items=new String[len][5];
      for(int i=0;i<len;i++)
      {
        items[i][0]=(String)data.elementAt(i*5);
        items[i][1]=(String)data.elementAt(i*5+1);
        items[i][2]=(String)data.elementAt(i*5+2);
        items[i][3]=(String)data.elementAt(i*5+3);
        items[i][4]=(String)data.elementAt(i*5+4);
      }
      gridControl1.setReadOnly(false);
      gridControl1.setItems(items);
      gridControl1.setReadOnly(true);
    }catch(Exception e)
    {
      System.out.println(e.toString());
    }
  }
  void button1_actionPerformed(ActionEvent e)
  {
     InputDialog idlg=new InputDialog(this,"添加新记录",true);
     idlg.setLocation(200,200);
     idlg.setSize(300,200);
     idlg.show();
  }

  void button2_actionPerformed(ActionEvent e)
  {
     InputDialog idlg=new InputDialog(this,"添加新记录",true);
     idlg.textField1.setEditable(false);
     idlg.groupBox1.setLabel("请输入修改用户信息:");
     idlg.textField1.setText((String)data.elementAt(row*5));
     idlg.textField2.setText((String)data.elementAt(row*5+1));
     idlg.textField3.setText((String)data.elementAt(row*5+2));
     idlg.textField4.setText((String)data.elementAt(row*5+3));
     idlg.textField5.setText((String)data.elementAt(row*5+4));
     idlg.setLocation(200,200);
     idlg.setSize(300,200);
     idlg.show();
  }

  void button3_actionPerformed(ActionEvent e)
  {
    try
    {
      MessageDialog mdlg=new MessageDialog(this,"删除确认","你确定要删除该记录吗?",6);
      mdlg.show();
      if(mdlg.getResult()==MessageDialog.NO)
        return;
      String name=(String)data.elementAt(row*5);
      String sql="delete from people where name='"+name+"'";
      statement=connection.prepareStatement(sql);
      statement.executeUpdate();
      statement.close();
      setGridControl();
    }catch(Exception ee)
    {
      System.out.println(ee.toString());
    }
  }

  void button4_actionPerformed(ActionEvent e)
  {
    System.exit(0);
  }

  void gridControl1_subfocusChanged(MatrixSubfocusEvent e)
  {
    try
    {
       MatrixLocation location=e.getLocation();
       if(location==null) return;
       row=location.row;
       column=location.column;
     }catch(Exception ee)
     { }
  }
}

class MainFrame_button1_actionAdapter implements java.awt.event.ActionListener
{
  MainFrame adaptee;

  MainFrame_button1_actionAdapter(MainFrame adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button1_actionPerformed(e);
  }
}

class MainFrame_button2_actionAdapter implements java.awt.event.ActionListener
{
  MainFrame adaptee;

  MainFrame_button2_actionAdapter(MainFrame adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button2_actionPerformed(e);
  }
}

class MainFrame_button3_actionAdapter implements java.awt.event.ActionListener
{
  MainFrame adaptee;

  MainFrame_button3_actionAdapter(MainFrame adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button3_actionPerformed(e);
  }
}

class MainFrame_button4_actionAdapter implements java.awt.event.ActionListener
{
  MainFrame adaptee;

  MainFrame_button4_actionAdapter(MainFrame adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button4_actionPerformed(e);
  }
}

class MainFrame_gridControl1_subfocusAdapter extends borland.jbcl.model.MatrixSubfocusAdapter
{
  MainFrame adaptee;

  MainFrame_gridControl1_subfocusAdapter(MainFrame adaptee)
  {
    this.adaptee = adaptee;
  }

  public void subfocusChanged(MatrixSubfocusEvent e)
  {
    adaptee.gridControl1_subfocusChanged(e);
  }
}

⌨️ 快捷键说明

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