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

📄 tableeditframe.java

📁 说明: 1、这是一个让人眼前一亮的设计
💻 JAVA
字号:
package jdbc_bible;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.EventObject;
import java.util.EventListener;
import java.util.Vector;
import javax.swing.table.*;
public class TableEditFrame extends JInternalFrame
{
	protected JTable table;
	protected JScrollPane tableScroller;
	protected JTextArea SQLPane=new JTextArea();
	protected JButton insertButton=new JButton("信息入库");
	protected DatabaseUtilities dbUtils;
	protected String tableName=null;
	protected String colNames[]=null;
	protected String dataTypes[]=null;
	protected String SQLCommand[]=null;
	protected String SQLCommandRoot=""; 
	protected String SQLCommandroot1="";
	public TableEditFrame(String tableName, DatabaseUtilities  dbUtils) 
	{
		this.setSize(630,420);
		this.setLocation(0,0);
		this.setClosable(true);
		this.setMaximizable(true);
		this.setIconifiable(true);
		this.setResizable(true);
		this.getContentPane().setLayout(new BorderLayout());
		this.tableName=tableName;
		this.dbUtils=dbUtils;
		this.SQLCommandRoot="INSERT INTO "+tableName+" VALUES";
		this.SQLCommandroot1="插入入库表中的值分别为 : ";
		this.setTitle("网上书店入库表单");
		init();
		this.setVisible(true);
	}	
	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);
		tableScroller=new JScrollPane(table);
		JSplitPane splitter=new JSplitPane(JSplitPane.VERTICAL_SPLIT,sqlScroller,tableScroller);
		splitter.setDividerLocation(100);
		this.getContentPane().add(splitter,BorderLayout.CENTER);
		this.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()
	{
		Vector vstr=new Vector();
		String[] a={"varchar","nvarchar","char","nchar","VARCHAR","CHAR"};
		for(int k=0;k<a.length;k++)
		{
			vstr.add(a[k]);			
		}	
		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=this.fixApostrophes(field);
						if(j>1)
							{	rowData+=",";}
							System.out.println(dataTypes[j]);
							
							if(vstr.contains(dataTypes[j]))
							rowData+="'"+field+"'";
						else
							rowData+=field;
					}
				}
				if(rowData.length()==0) break;
				tableValues.addElement("("+rowData+");\n");
			}
		}
		return tableValues;
	}
	public Vector parseTable1()
	{
		int rows=table.getRowCount();
		int cols=table.getColumnCount();
		Vector tableValues=new Vector();
		Vector rowDataa=new Vector();
		
		if(rows>=0&&cols>=0)
		{
			for(int i=0;i<rows; i++)
			{
				
				for(int j=1;j<cols;j++)
				{
					String field=(String)table.getValueAt(i,j);
					if(field.length()>0)
					{
						field=this.fixApostrophes(field);
						rowDataa.addElement(field);
					}
				}
				if(rowDataa.size()==0) break;
				tableValues.add(rowDataa);
			}
		}
		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;
	}
	
	class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String[] colNames2=null;
			boolean f=false;
			colNames2=colNames;
			System.out.println(colNames2[0]+colNames2[1]);
			f=dbUtils.execute(SQLCommand);
			if(f)
			{
				JOptionPane.showMessageDialog(null,"数据添加成功");
				SQLPane.setText("");
				JViewport viewport=tableScroller.getViewport();
				System.out.println("first");
				viewport.remove(table);
				System.out.println("second");
				table=createTable(colNames,15);
				System.out.println("third");
				viewport.add(table);
				TableChangeListener modelListener=new TableChangeListener();
		table.getModel().addTableModelListener(modelListener);
				System.out.println("forth");
			}
			else
			{
				JOptionPane.showMessageDialog(null,"数据添加失败");
			}		
		}
	}
	
	class TableChangeListener implements TableModelListener	
	{
		String[] a=null;
		String[] b=null;
		public TableChangeListener()
		{
		}
		public void tableChanged(TableModelEvent event)
		{
			Vector rowData=parseTable();
			Vector rowData2=parseTable1();
			SQLCommand=new String[rowData.size()];
			a=new String[rowData2.size()];
			b=new String[rowData2.size()];
			SQLPane.setText("");
			for(int i=0;i<rowData.size();i++)
			{
				if(rowData.elementAt(i)==null)
				break;
				System.out.println(rowData.elementAt(i).toString());
				SQLCommand[i]=SQLCommandRoot+(String)rowData.elementAt(i);
				Vector colData=(Vector)rowData2.elementAt(i);
				for(int j=0;j<colData.size();j++)
				{
					b[i]+=colNames[j+1]+" : "+(String)colData.elementAt(j);
					if(j<(colData.size()-1))
					b[i]+=" , ";
				}
				a[i]=SQLCommandroot1+b[i];
				SQLPane.append(a[i]+" ;");
			}
		}
	}
}

⌨️ 快捷键说明

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