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

📄 tableeditframe.java

📁 用java编写的数据库管理程序
💻 JAVA
字号:
package createdb;import java.awt.*;import java.awt.event.*;import java.util.EventObject;import java.util.EventListener;import java.util.Vector;import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;/*** TableEditFrame extends JInternalFrame to create a display which builds* SQL CREATE statements* <p/>* TableBuilder, which extends JTable, is a key component.*/class TableEditFrame extends JInternalFrame{  protected JTable table;  protected JTextArea SQLPane = new JTextArea();  protected JButton insertButton = new JButton("Insert Data");  protected DatabaseUtilities dbUtils;  protected String tableName = null;  protected String colNames[] = null;  protected String dataTypes[] = null;  protected String SQLCommand[] = null;  protected String SQLCommandRoot = "";  public TableEditFrame(String tableName, DatabaseUtilities dbUtils){    setSize(600,400);    setLocation(10,10);    setClosable(true);    setMaximizable(true);    setIconifiable(true);    setResizable(true);    getContentPane().setLayout(new BorderLayout());    this.tableName=tableName;    this.dbUtils=dbUtils;    SQLCommandRoot = "INSERT INTO "+tableName+" VALUES ";    setTitle(SQLCommandRoot);    init();    setVisible(true);  }  // initialise the JInternalFrame  private void init(){    colNames = dbUtils.getColumnNames(tableName);    dataTypes = dbUtils.getDataTypes(tableName);    table = createTable(colNames,15);    TableChangeListener modelListener = new TableChangeListener();    table.getModel().addTableModelListener(modelListener);    JScrollPane sqlScroller = new JScrollPane(SQLPane);    JScrollPane tableScroller = new JScrollPane(table);    JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT,                          sqlScroller,tableScroller);    splitter.setDividerLocation(100);    getContentPane().add(splitter,BorderLayout.CENTER);    getContentPane().add(insertButton,BorderLayout.SOUTH);    insertButton.addActionListener(new ButtonListener());  }  protected JTable createTable(String[] colNames,int nRows){    String[][] rowData = new String[nRows][colNames.length];    for(int i=0;i<nRows;i++){      for(int j=0;j<colNames.length;j++)rowData[i][j]="";    }    JTable table = new JTable(rowData,colNames);    return table;  }  public Vector parseTable(){    int rows = table.getRowCount();    int cols = table.getColumnCount();    Vector tableValues = new Vector();    if(rows>=0&&cols>=0){      for(int i=0;i<rows;i++){        String rowData = "";        for(int j=0;j<cols;j++){          String field = (String)table.getValueAt(i,j);          if(field.length()>0){            field = fixApostrophes(field);            if(j>0)rowData += ", ";            if(dataTypes[j].equalsIgnoreCase("CHAR")||               dataTypes[j].equalsIgnoreCase("VARCHAR")||               dataTypes[j].equalsIgnoreCase("NCHAR"))              rowData += "'"+field+"'";            else              rowData += field;          }        }        if(rowData.length()==0)break;        tableValues.addElement(" ( " + rowData + " );\n");      }    }    return tableValues;  }  private String fixApostrophes(String in){    int n=0;    while((n=in.indexOf("'",n))>=0){      in = in.substring(0,n)+"'"+in.substring(n);      n+=2;    }    return in;  }  // Listener for the Insert Button  class ButtonListener implements ActionListener{    public void actionPerformed(ActionEvent event){      dbUtils.execute(SQLCommand);    }  }  // Listener for Edit events on the JTable  class TableChangeListener implements TableModelListener{    public TableChangeListener(){    }    public void tableChanged(TableModelEvent event){      Vector rowData = parseTable();      SQLCommand = new String[rowData.size()];      SQLPane.setText("");      for(int i=0;i<rowData.size();i++){        if(rowData.elementAt(i)==null)break;        SQLCommand[i] = SQLCommandRoot+(String)rowData.elementAt(i);        SQLPane.append(SQLCommand[i]);      }    }  }}

⌨️ 快捷键说明

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