📄 tablequeryframe.java
字号:
package jdbc_bible;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;
public class TableQueryFrame extends JInternalFrame
{
protected JTable table;
protected JScrollPane tableScroller;
protected JTextArea SQLPane=new JTextArea();
protected JButton queryButton=new JButton("Execute Query ");
protected DatabaseUtilities dbUtils;
protected String tableName=null;
protected String colNames[]=null;
protected String dataTypes[]=null;
protected String SQLQuery=null;
protected String SQLCommandRoot=" ";
public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
System.out.println(tableName+","+dbUtils);
this.setSize(600,400);
this.setLocation(10,10);
this.setClosable(true);
this.setMaximizable(true);
this.setIconifiable(true);
this.setResizable(true);
this.getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;
this.setTitle("Query"+tableName);
init();
this.setVisible(true);
}
private void init()
{
colNames=dbUtils.getColumnNames(tableName);
dataTypes=dbUtils.getDataTypes(tableName);
System.out.println("tableName"+tableName);
SQLQuery="SELECT TOP 5 * FROM "+tableName;
System.out.println("SQLQuery"+SQLQuery);
Vector dataSet=dbUtils.executeQuery(SQLQuery);
System.out.println("colNames"+colNames.toString()+"dataSet"+dataSet.toString());
table=createTable(colNames,dataSet);
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(queryButton,BorderLayout.SOUTH);
queryButton.addActionListener(new ButtonListener());
}
protected JTable createTable(String[] colNames,Vector dataSet)
{
int nRows=dataSet.size();
String[][] rowData=new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row=(Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table=new JTable(rowData,colNames);
return table;
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
SQLQuery=SQLPane.getText();
JViewport viewport=tableScroller.getViewport();
viewport.remove(table);
colNames=dbUtils.getColumnNamesUsingQuery(SQLQuery);
Vector dataSet=dbUtils.executeQuery(SQLQuery);
table=createTable(colNames,dataSet);
viewport.add(table);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -