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

📄 表格.txt

📁 用Eclipse里的SWT的Table控件实现对数据库的连接
💻 TXT
字号:
package com.fengmanfei.ch9;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import java.sql.*;

public class TableSample {
	private Shell sShell=null;
	private ViewForm viewForm=null;
	private Composite composite=null;
	private Table table=null;
	
	private void createViewForm(){
		viewForm=new ViewForm(sShell,SWT.NONE);
		viewForm.setTopCenterSeparate(true);
		createComposite();
		viewForm.setContent(composite);
		sShell.setSize(600,800);
	}
	private void createComposite(){
		GridLayout gridLayout=new GridLayout();
		gridLayout.numColumns=1;
		composite=new Composite(viewForm,SWT.NONE);
		composite.setLayout(gridLayout);
		createTable();
	}
	private void createTable(){
		GridData gridData=new org.eclipse.swt.layout.GridData();
		gridData.horizontalAlignment=SWT.FILL;
		gridData.grabExcessHorizontalSpace=true;
		gridData.grabExcessVerticalSpace=true;
		gridData.verticalAlignment=SWT.FILL;
		table=new Table(composite,SWT.HIDE_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(gridData);		
		String[] tableHeader={"学号","姓名","年龄","家乡","部门"};
		
		for(int i=0;i<tableHeader.length;i++){
			TableColumn tableColumn=new TableColumn(table,SWT.NONE);
			tableColumn.setText(tableHeader[i]);
		}
	}
	
	public void main(String[] args){
		Display display=Display.getDefault();
		TableSample thisClass=new TableSample();
		thisClass.createSShell();
		thisClass.sShell.open();
		while(!thisClass.sShell.isDisposed()){
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		display.dispose();
		
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection("jdbc:odbc:test");
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery("SELECT * FROM student");
			while(rs.next())
			{				
				TableItem item=new TableItem(table,SWT.NONE);
				item.setText(new String[]{rs.getString("id"),rs.getString("name"),rs.getString("age"),rs.getString("home"),rs.getString("department")});
			}
			rs.close();
			stmt.close();
			con.close();
		}
		catch(ClassNotFoundException e)
		{
			System.out.println("错误: "+e);
		}
		catch(SQLException e)
		{
			System.out.println("错误: "+e);
		}
	}
	private void createSShell(){
		sShell=new Shell();
		sShell.setText("数据库");
		sShell.setLayout(new FillLayout());
		createViewForm();
		sShell.pack();
	}
}

⌨️ 快捷键说明

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