回 帖 发 新 帖 刷新版面

主题:[讨论]SWT table没有显示数据

SQL原文件

use luo

select * from student
drop table student

create table student
(
ID int primary key,
NAME varchar (15),
SEX varchar(10),
AGE varchar(10)
)

select * from student


insert into student values ('1','aa','n','20')
insert into student values ('2','bb','v','25')
insert into student values ('3','cc','n','21')
insert into student values ('4','dd','v','19')
insert into student values ('5','ee','n','27')



//student基本资料

package new2;

public class student {
    private int ID;
    private String NAME;
    private String SEX;
    private String AGE;
    
    public int getID() {
        return ID;
    }
    public void setID(int ID) {
        this.ID = ID;
    }
    public String getNAME() {
        return NAME;
    }
    public void setNAME(String NAME) {
        this.NAME = NAME;
    }
    public String getSEX() {
        return SEX;
    }
    public void setSEX(String SEX) {
        this.SEX = SEX;
    }
    public String getAGE() {
        return AGE;
    }
    public void setAGE(String AGE) {
        this.AGE = AGE;
    }
    
}

回复列表 (共3个回复)

沙发

//JAVA连接的SQL

package new2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class sql {
    public List aa()
    {
        ArrayList<student> list =new ArrayList<student>();
        
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
        String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Luo";
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            con = DriverManager.getConnection(url, "sa", "sa");
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from student");
            while(rs.next())
            {
            student stu = new student();
            int a = rs.getInt(1); 
            String b = rs.getString(2);
            String c = rs.getString(3);
            String d = rs.getString(4);
            stu.setID(a);
            stu.setNAME(b);
            stu.setSEX(c);
            stu.setSEX(d);
            list.add(stu);
            
            }
                //System.out.println(result);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println(e.getErrorCode());
            e.printStackTrace();
        }finally{
            try {
                if (stmt != null)
                    stmt.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }    
        }
        return list;
    }

}






板凳

//SWT table代码

package new2;

。。。。。。。。。。。。。。。
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import new2.student;
public class stu2 extends Shell {

    private Text text_1;
    private Table table_1;
    private String[] titles = { "学号", "姓名", "性别", "年龄" };
    private sql sq = new sql();
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            stu2 shell = new stu2(display, SWT.SHELL_TRIM);
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the shell
     * @param display
     * @param style
     */
    public stu2(Display display, int style) {
        super(display, style);
        createContents();
    }

    /**
     * Create contents of the window
     */
    protected void createContents() {
        setText("SWT Application");
        setSize(500, 375);

        final Menu menu = new Menu(this, SWT.BAR);
        setMenuBar(menu);

        final CTabFolder tabFolder = new CTabFolder(this, SWT.NONE);
        tabFolder.setBounds(0, 0, 492, 321);

        final CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
        tabItem.setText("查找");

        final Composite composite = new Composite(tabFolder, SWT.NONE);
        tabItem.setControl(composite);

        table_1 = new Table(composite, SWT.BORDER);
        table_1.setBounds(0, 106, 488, 194);
        table_1.setLinesVisible(true);
        table_1.setHeaderVisible(true);

        final TableColumn col = new TableColumn(table_1, SWT.NONE);
        col.setText("学号");

    

3 楼


///接上面代码

    final TableColumn col_2 = new TableColumn(table_1, SWT.NONE);
        col_2.setText("姓名");

        final TableColumn col_3 = new TableColumn(table_1, SWT.NONE);
        col_3.setText("性别");

        final TableColumn col_4 = new TableColumn(table_1, SWT.NONE);
        col_4.setText("年龄");

        final Button button = new Button(composite, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                table_1.removeAll();
                List<student> all = new ArrayList<student>();
                int n = all.size();
                all = sq.aa();
                TableColumn[] cols = table_1.getColumns();
            
                for (int i = 0; i < titles.length; i++) {
                    table_1.getColumn(i).pack();
                }
                table_1.getColumn(0).setWidth(120);
                table_1.getColumn(1).setWidth(120);
                table_1.getColumn(2).setWidth(120);
                table_1.getColumn(3).setWidth(120);
                
                for(int i = 0; i < n; i++){
                    student tb = all.get(i);
                    showinfo(tb);
                }
            
            }

            private void showinfo(student tb) {                TableItem item = new TableItem(table_1,SWT.BORDER);
                item.setText(0, Integer.toString(tb.getID()));
                item.setText(1, tb.getNAME());
                item.setText(2, tb.getSEX());
                item.setText(3, tb.getAGE());
                }});
        button.setText("确定");
        button.setBounds(213, 45, 48, 22);

        final Label label = new Label(composite, SWT.NONE);
        label.setText("完全查找 ");
        label.setBounds(134, 50, 54, 12);

        text_1 = new Text(composite, SWT.BORDER);
        text_1.setBounds(300, 5, 176, 100);
        }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

}

我来回复

您尚未登录,请登录后再回复。点此登录或注册