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

📄 tableexample.java

📁 本系统是一套基于WEB的软件平台。借助于现代信息技术手段
💻 JAVA
字号:
/* * @(#)TableExample.java	1.15 01/12/03 * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//** * A a UI around the JDBCAdaptor, allowing database data to be interactively * fetched, sorted and displayed using Swing. * * NOTE: This example uses a modal dialog via the static convenience methods in * the JOptionPane. Use of modal dialogs requires JDK 1.1.4 or greater. * * @version 1.15 12/03/01 * @author Philip Milne */import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import javax.swing.event.*;import javax.swing.border.*;import java.io.*;public class TableExample implements LayoutManager {    static String[] ConnectOptionNames = { "Connect" };    static String   ConnectTitle = "Connection Information";    Dimension   origin = new Dimension(0, 0);    JButton     fetchButton;    JButton     showConnectionInfoButton;    JPanel      connectionPanel;    JFrame      frame; // The query/results window.    JLabel      userNameLabel;    JTextField  userNameField;    JLabel      passwordLabel;    JTextField  passwordField;    // JLabel      queryLabel;    JTextArea   queryTextArea;    JComponent  queryAggregate;    JLabel      serverLabel;    JTextField  serverField;    JLabel      driverLabel;    JTextField  driverField;    JLabel      FileNameLabel;    JTextField  FileNameField;    JPanel      mainPanel;    TableSorter sorter;    JDBCAdapter dataBase;    JScrollPane tableAggregate;    String  FileName;     PrintWriter log;    /**     * Brigs up a JDialog using JOptionPane containing the connectionPanel.     * If the user clicks on the 'Connect' button the connection is reset.     */    void activateConnectionDialog() {	if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle,		   JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,                   null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {	    connect();            frame.setVisible(true);	}	else if(!frame.isVisible())	    System.exit(0);    }    /**     * Creates the connectionPanel, which will contain all the fields for     * the connection information.     */    public void createConnectionDialog() { 	// Create the labels and text fields.	userNameLabel = new JLabel("User name: ", JLabel.RIGHT); 	userNameField = new JTextField("sa");	passwordLabel = new JLabel("Password: ", JLabel.RIGHT);	passwordField = new JTextField("sa");        serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);	serverField = new JTextField("jdbc:odbc:JXC");	driverLabel = new JLabel("Driver: ", JLabel.RIGHT);	driverField = new JTextField("sun.jdbc.odbc.JdbcOdbcDriver");    FileNameLabel= new JLabel("文件名: ", JLabel.RIGHT);    FileNameField= new JTextField("XX");	connectionPanel = new JPanel(false);	connectionPanel.setLayout(new BoxLayout(connectionPanel,						BoxLayout.X_AXIS));	JPanel namePanel = new JPanel(false);	namePanel.setLayout(new GridLayout(0, 1));	namePanel.add(userNameLabel);	namePanel.add(passwordLabel);	namePanel.add(serverLabel);	namePanel.add(driverLabel);    namePanel.add(FileNameLabel);	JPanel fieldPanel = new JPanel(false);	fieldPanel.setLayout(new GridLayout(0, 1));	fieldPanel.add(userNameField);	fieldPanel.add(passwordField);	fieldPanel.add(serverField);    fieldPanel.add(driverField);    fieldPanel.add(FileNameField);	connectionPanel.add(namePanel);	connectionPanel.add(fieldPanel);    }    public TableExample() {        mainPanel = new JPanel();        // Create the panel for the connection information	createConnectionDialog();	// Create the buttons.	showConnectionInfoButton = new JButton("Configuration");        showConnectionInfoButton.addActionListener(new ActionListener() {	        public void actionPerformed(ActionEvent e) {	            activateConnectionDialog();	        }	    }	);	fetchButton = new JButton("Fetch");        fetchButton.addActionListener(new ActionListener() {	        public void actionPerformed(ActionEvent e) {	            fetch();	        }	    }	);	// Create the query text area and label.        queryTextArea = new JTextArea("SELECT * FROM uses", 25, 25);	queryAggregate = new JScrollPane(queryTextArea);        queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));        // Create the table.        tableAggregate = createTable();        tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED));	// Add all the components to the main panel.        mainPanel.add(fetchButton);        mainPanel.add(showConnectionInfoButton);        mainPanel.add(queryAggregate);        mainPanel.add(tableAggregate);        mainPanel.setLayout(this);        // Create a Frame and put the main panel in it.        frame = new JFrame("TableExample");        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {System.exit(0);}});        frame.setBackground(Color.lightGray);        frame.getContentPane().add(mainPanel);        frame.pack();        frame.setVisible(false);        frame.setBounds(200, 200, 640, 480);	activateConnectionDialog();    }    public void connect() {       dataBase = new JDBCAdapter(            serverField.getText(),            driverField.getText(),            userNameField.getText(),            passwordField.getText());           FileName=FileNameField.getText();       sorter.setModel(dataBase);   }    public void fetch() {        dataBase.executeQuery(queryTextArea.getText());       	try {		log = new PrintWriter(new FileWriter(FileName+".java", true), true);		}		catch (IOException e) {		System.err.println("无法创建日志文件: " + e);		}        BornTop();		BornALL();		Bornexcute();		BornEdit();		BornAdd();    	BornDel();    }    	public void BornDel() {		log.println("  public void Del() throws Exception {");		log.println("    String Str=\"Delete From "+FileName+" where Id=\"+"+dataBase.getColumnName(0)+";");		log.println("    DBConnect dbc  = new DBConnect();");		log.println("    dbc.executeUpdate(Str);");		log.println("    dbc.close();");		log.println("   }");		log.println("");		log.println("  }");	}    public void BornTop() { 		log.println("package JXC.web;");		log.println("import JXC.Com.DBConnect;");		log.println("import java.lang.String;");		log.println("import JXC.util.StrFun;");		log.println("import java.sql.*;");		log.println("import javax.servlet.*;");		log.println("import javax.servlet.http.*;");		log.println("/**");		log.println(" * Title:       明细单信息");		log.println(" * Description:");		log.println(" * Copyright:    Copyright (c) 2003");		log.println(" * Company:      torch");		log.println(" * @author:		 wind");		log.println(" * @version 1.0");		log.println(" */");    }    public void BornALL() { 		log.println("public class "+FileName +"{"); 		for (int i=0;i<dataBase.getColumnCount();i++)		  log.println("  private "+dataBase.getColumnClassStr(i)+" "+dataBase.getColumnName(i)+";");		log.println("  public void "+FileName +"(){}");		for (int i=0;i<dataBase.getColumnCount();i++) {		 String Fieldtype=dataBase.getColumnClassStr(i);		 String Fieldname=dataBase.getColumnName(i);		 log.println("  public "+Fieldtype+"  get"+LowerOne(Fieldname)+"() {");		 log.println("    return "+Fieldname+";");	     log.println("  }");	   	 log.println("  public void set"+LowerOne(Fieldname)+"("+Fieldtype+" anew) {");		 log.println("    this."+Fieldname+"=anew;");	     log.println("  }");	     if (i==0) {	   	   log.println("  public void set"+LowerOne(Fieldname)+"(String anew) {"); 	       log.println("     if (anew!=null)");		   log.println("    this."+Fieldname+"=Integer.parseInt(anew);");	       log.println("  }");	     		      }		}    }    public void Bornexcute(){      log.println("  public boolean excute() throws Exception {"); 	  log.println("	   String Str=\"Select * From "+FileName+" where Id=\"+"+dataBase.getColumnName(0)+";");	  log.println("    try {");	  log.println("	   DBConnect dbc  = new DBConnect(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);");	  log.println("	   ResultSet rs = dbc.executeQuery(Str);");      log.println("    if (rs.next()){");      for (int i=0;i<dataBase.getColumnCount();i++){        String Fieldtype=LowerOne(dataBase.getColumnClassStr(i));        String Fieldname=dataBase.getColumnName(i);        log.println("     "+Fieldname+"=rs.get"+Fieldtype+"(\""+Fieldname+"\"); ");       }   	  log.println("	  }");	  log.println("    rs.close();");	  log.println("    dbc.close();");	  log.println("    return true;");	  log.println("    }");	  log.println("    catch (SQLException sqle){");	  log.println("	    return false;");	  log.println("    }");	  log.println("  } ");    }    public void BornEdit(){      log.println("  public void Edit(HttpServletRequest request) throws Exception {");      String Str="String Str=\"Update "+FileName+" set ";      log.println("   "+dataBase.getColumnName(0)+"=StrFun.getInt(request,\"id\"); ");      for (int i=1;i<dataBase.getColumnCount();i++){        String Fieldtype=LowerOne(dataBase.getColumnClassStr(i));        String Fieldname=dataBase.getColumnName(i);        String aa="";        if (Fieldtype.equals("String")) {          Str=Str+Fieldname+"='\"+"+Fieldname+"+\"' ";          aa="String";        }        else {        	if (Fieldtype.equals("Date")){        	  Str=Str+Fieldname+"=#\"+"+Fieldname+"+\"# ";        	  aa="String";        	 }        	else {        	  Str=Str+Fieldname+"=\"+"+Fieldname+"+\" ";              if (Fieldtype.equals("Int"))                aa="Int";              else {                if (Fieldtype.equals("Float"))                aa="Float";        	}        }       }        log.println("   "+Fieldname+"=StrFun.get"+aa+"(request,\""+Fieldname+"\"); ");        if (i!=dataBase.getColumnCount()-1)          Str=Str+",";        else          Str=Str+" where id=\"+"+dataBase.getColumnName(0);      }      log.println("   "+Str+";");      log.println("   DBConnect dbc  = new DBConnect();");	  log.println("   dbc.executeUpdate(Str);");      log.println("   dbc.close();");      log.println("    }");    }    public void BornAdd(){      log.println("  public void Add(HttpServletRequest request) throws Exception {");    //  log.println("   "+dataBase.getColumnName(0)+"=StrFun.getString(request,\""+dataBase.getColumnName(0)+"\"); ");      String Str="String Str=\"Insert into "+FileName+"(";      String Str1="";      for (int i=1;i<dataBase.getColumnCount();i++){        String Fieldtype=LowerOne(dataBase.getColumnClassStr(i));        String Fieldname=dataBase.getColumnName(i);        String aa="String";        if (Fieldtype.equals("Int"))          aa="Int";        if (Fieldtype.equals("Float"))          aa="Float";        log.println("   "+Fieldname+"=StrFun.get"+aa+"(request,\""+Fieldname+"\"); ");        Str=Str+Fieldname;        if (i!=dataBase.getColumnCount()-1)          Str=Str+",";        else          Str=Str+") values(";        if (Fieldtype.equals("String"))        Str1=Str1+"'\"+"+Fieldname+"+\"'";        else {        	if (Fieldtype.equals("Date"))        	  Str1=Str1+"#\"+"+Fieldname+"+\"#";        	else        	  Str1=Str1+"\"+"+Fieldname+"+\"";        }        if (i!=dataBase.getColumnCount()-1)          Str1=Str1+",";        else          Str1=Str1+")\"";      }      log.println("   "+Str+Str1+";");      log.println("   DBConnect dbc  = new DBConnect();");	  log.println("   dbc.executeUpdate(Str);");      log.println("   dbc.close();");      log.println("    }");    }    private String LowerOne(String aa){      String bb=aa;      String cc=bb.substring(0,1);        bb=bb.substring(1);       return cc.toUpperCase()+bb;    }    public JScrollPane createTable() {        sorter = new TableSorter();        //connect();        //fetch();        // Create the table        JTable table = new JTable(sorter);	// Use a scrollbar, in case there are many columns.	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);        // Install a mouse listener in the TableHeader as the sorter UI.        sorter.addMouseListenerToHeaderInTable(table);        JScrollPane scrollpane = new JScrollPane(table);        return scrollpane;    }    public static void main(String s[]) {        new TableExample();    }    public Dimension preferredLayoutSize(Container c){return origin;}    public Dimension minimumLayoutSize(Container c){return origin;}    public void addLayoutComponent(String s, Component c) {}    public void removeLayoutComponent(Component c) {}    public void layoutContainer(Container c) {        Rectangle b = c.getBounds();        int topHeight = 90;        int inset = 4;        showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25);        fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25);        // queryLabel.setBounds(10, 10, 100, 25);        queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80);        tableAggregate.setBounds(new Rectangle(inset,                                               inset + topHeight,                                               b.width-2*inset,                                               b.height-2*inset - topHeight));    }}

⌨️ 快捷键说明

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