JAVA用SWT写的界面,怎样把数据库里面的表显示在SWT的table中?并且实现修改查询删除功能。
顺便举个例子说明下TableViewer怎么应用最好给咱一段简单的的代码,举个例子,不明白啊~~TableViewer这个东西,一共就15分了~~求给力~~~...
顺便举个例子说明下TableViewer怎么应用最好给咱一段简单的的代码,举个例子,不明白啊~~TableViewer这个东西,一共就15分了~~求给力~~~
展开
展开全部
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TabViewTest {
public void open() throws Exception{
Display display=new Display();
Shell shell=new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(new Point(400,300));
Table table=new Table(shell,SWT.MULTI | SWT.FULL_SELECTION |SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn idColumn =new TableColumn(table,SWT.LEFT);
idColumn.setText("id");
idColumn.setWidth(100);
TableColumn usernameColumn=new TableColumn(table,SWT.LEFT);
usernameColumn.setText("username");
usernameColumn.setWidth(100);
TableColumn passwordColumn=new TableColumn(table,SWT.LEFT);
passwordColumn.setText("password");
passwordColumn.setWidth(100);
Connection conn=getAcessConnection();
String sqlString="select * from user";
Statement statement=conn.createStatement();
ResultSet rSet=statement.executeQuery(sqlString);
while(rSet.next()){
TableItem item=new TableItem(table,SWT.LEFT);
String[] valueStrings={rSet.getInt("id")+"0",rSet.getString("username"),rSet.getString("password")};
item.setText(valueStrings);
}
rSet.close();
statement.close();
conn.close();
shell.open();
while(!shell.isDisposed()){
if( !display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) throws Exception {
new TabViewTest().open();
}
public Connection getAcessConnection(){
Connection conn=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
URL url1=TabViewTest.class.getClassLoader().getResource("db/test.mdb");
String path=url1.getPath();
path=path.substring(1, path.length());
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+path;
conn=DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
数据库用的是access,楼主可以自己随便修改下的。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TabViewTest {
public void open() throws Exception{
Display display=new Display();
Shell shell=new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(new Point(400,300));
Table table=new Table(shell,SWT.MULTI | SWT.FULL_SELECTION |SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn idColumn =new TableColumn(table,SWT.LEFT);
idColumn.setText("id");
idColumn.setWidth(100);
TableColumn usernameColumn=new TableColumn(table,SWT.LEFT);
usernameColumn.setText("username");
usernameColumn.setWidth(100);
TableColumn passwordColumn=new TableColumn(table,SWT.LEFT);
passwordColumn.setText("password");
passwordColumn.setWidth(100);
Connection conn=getAcessConnection();
String sqlString="select * from user";
Statement statement=conn.createStatement();
ResultSet rSet=statement.executeQuery(sqlString);
while(rSet.next()){
TableItem item=new TableItem(table,SWT.LEFT);
String[] valueStrings={rSet.getInt("id")+"0",rSet.getString("username"),rSet.getString("password")};
item.setText(valueStrings);
}
rSet.close();
statement.close();
conn.close();
shell.open();
while(!shell.isDisposed()){
if( !display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) throws Exception {
new TabViewTest().open();
}
public Connection getAcessConnection(){
Connection conn=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
URL url1=TabViewTest.class.getClassLoader().getResource("db/test.mdb");
String path=url1.getPath();
path=path.substring(1, path.length());
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+path;
conn=DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
数据库用的是access,楼主可以自己随便修改下的。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |