用SWT中的TableEidtor输入数据后,如何通过JDBC提交到数据库中

问题描述

import org.eclipse.swt.SWT;import org.eclipse.swt.custom.TableEditor;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;public class TableEditorExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER | SWT.MULTI); table.setLinesVisible(true); for (int i = 0; i < 3; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setWidth(100); } for (int i = 0; i < 3; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "" + i, "" + i, "" + i }); } final TableEditor editor = new TableEditor(table); editor.horizontalAlignment = SWT.LEFT; editor.grabHorizontal = true; table.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event event) { Rectangle clientArea = table.getClientArea(); Point pt = new Point(event.x, event.y); int index = table.getTopIndex(); while (index < table.getItemCount()) { boolean visible = false; final TableItem item = table.getItem(index); for (int i = 0; i < table.getColumnCount(); i++) { Rectangle rect = item.getBounds(i); if (rect.contains(pt)) { final int column = i; final Text text = new Text(table, SWT.NONE); Listener textListener = new Listener() { public void handleEvent(final Event e) { switch (e.type) { case SWT.FocusOut: item.setText(column, text.getText()); text.dispose(); break; case SWT.Traverse: switch (e.detail) { case SWT.TRAVERSE_RETURN: item .setText(column, text .getText()); //FALL THROUGH case SWT.TRAVERSE_ESCAPE: text.dispose(); e.doit = false; } break; } } }; text.addListener(SWT.FocusOut, textListener); text.addListener(SWT.Traverse, textListener); editor.setEditor(text, item, i); text.setText(item.getText(i)); text.selectAll(); text.setFocus(); return; } if (!visible && rect.intersects(clientArea)) { visible = true; } } if (!visible) return; index++; } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }}这段代码作用是用来给我的Table上添加文本框给用户输入,是我从网上找到的一段代码,但是现在不知道如何通过JDBC将输入的内容添加到数据库表中(假设有ID,Name,Age三个字段)。谢谢您的回复!

解决方案

package test;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.TableEditor;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;public class TestTableEditor {public static void main(String[] args) {Display display = new Display();Shell shell = new Shell(display);shell.setLayout(new FillLayout());final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);final Button button = new Button(shell, SWT.BORDER | SWT.MULTI);button.setText("Save");button.addListener(SWT.MouseDown, new Listener() {@Overridepublic void handleEvent(Event arg0) {TableItem[] items = table.getItems();for(TableItem ti : items) {String id = ti.getText(0);String name = ti.getText(1);String age = ti.getText(2);}}});table.setLinesVisible(true);for (int i = 0; i < 3; i++) {TableColumn column = new TableColumn(table, SWT.NONE);column.setWidth(100);}for (int i = 0; i < 3; i++) {TableItem item = new TableItem(table, SWT.NONE);item.setText(new String[] { "" + i, "" + i, "" + i });}final TableEditor editor = new TableEditor(table);editor.horizontalAlignment = SWT.LEFT;editor.grabHorizontal = true;table.addListener(SWT.MouseDown, new Listener() {public void handleEvent(Event event) {Rectangle clientArea = table.getClientArea();Point pt = new Point(event.x, event.y);int index = table.getTopIndex();while (index < table.getItemCount()) {boolean visible = false;final TableItem item = table.getItem(index);for (int i = 0; i < table.getColumnCount(); i++) {Rectangle rect = item.getBounds(i);if (rect.contains(pt)) {final int column = i;final Text text = new Text(table, SWT.NONE);Listener textListener = new Listener() {public void handleEvent(final Event e) {switch (e.type) {case SWT.FocusOut:item.setText(column, text.getText());text.dispose();break;case SWT.Traverse:switch (e.detail) {case SWT.TRAVERSE_RETURN:item.setText(column, text.getText());// FALL THROUGHcase SWT.TRAVERSE_ESCAPE:text.dispose();e.doit = false;}break;}}};text.addListener(SWT.FocusOut, textListener);text.addListener(SWT.Traverse, textListener);editor.setEditor(text, item, i);text.setText(item.getText(i));text.selectAll();text.setFocus();return;}if (!visible && rect.intersects(clientArea)) {visible = true;}}if (!visible)return;index++;}}});shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}display.dispose();}}你的代码,我加上了button按钮,点击之后,会得到所有table数据,这时候你就可以使用jdbc插入数据库了。

时间: 2024-08-03 13:27:23

用SWT中的TableEidtor输入数据后,如何通过JDBC提交到数据库中的相关文章

js-JS从单选框中选择一个值后,点击提交后显示出该值!请问我的代码怎么修改啊啊?

问题描述 JS从单选框中选择一个值后,点击提交后显示出该值!请问我的代码怎么修改啊啊? <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" language="javascript

系统日志的编写-使用C#语言实现系统日志的记录,即把用户名及用户登录后的所有操作记录在数据库中。主要是怎么实

问题描述 使用C#语言实现系统日志的记录,即把用户名及用户登录后的所有操作记录在数据库中.主要是怎么实 使用C#语言实现系统日志的记录,即把用户名及用户登录后的所有操作记录在数据库中.主要是怎么实现? 解决方案 有很多方法,比如直接就插入数据库,用aop日志框架,数据库用触发器等等. 解决方案二: 用log4Net就可以轻松实现你的要求!! 解决方案三: 是的,以上两种方法均可以,但是具体的点击某一个菜单,记录此操作的id等,有没有具体的代码实现的呀? 解决方案四: 建立一个日志表,使用inse

net menu 动态获取-Ext.net 2.5 中想要实现menu及子菜单动态从数据库中获取,如何实现

问题描述 Ext.net 2.5 中想要实现menu及子菜单动态从数据库中获取,如何实现 解决方案 看ext.net的例子代码,然后将写死的菜单数据部分修改为从数据库读取就可以了.

怎么在程序中包含图形统计报表,统计数据需要从数据库中读出。

问题描述 怎么在winform程序中包含图形统计报表,统计数据需要从数据库中读出. 解决方案 解决方案二:zedgraph你去网上搜搜看解决方案三:找过,不过没有找到.

新手求教如何把textbox中的值通过按钮提交到数据库中

问题描述 这是一个简单的注册页面我该如何将里面的输入的值,通过按钮实现,提交到数据库中呢,我是个超级白痴--,啥都不懂,大神们任意回答把 解决方案 解决方案二:1:熟悉asp.net的常用控件.2:如果是Sqlserver数据库搜索有关Sqlserver的CRUD操作.3:搜索C#ADO.Net操作数据库.解决方案三:请先基础学习,没基础就上手是危楼解决方案四:stringsqlstr=System.Configuration.ConfigurationManager.AppSettings["

JDBC程序更新数据库中记录的方法_java

本文实例讲述了JDBC程序更新数据库中记录的方法.分享给大家供大家参考,具体如下: 使用JDBC程序(Eclipse.MyEclipse)更新数据库(MySql)中的记录时可以只修改记录的一个字段或几个字段,具体方法为可以加入如下被注释代码(前提是修改之前可以从数据库中得到该条记录)以user表为例 public class UserDaoJdbcImpl implements UserDao { public void update(User u) { Connection conn = nu

使用JDBC在MySQL数据库中如何快速批量插入数据_Mysql

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch() throws SQLException Adds a set of parameters to this PreparedStatement object's batch of commands. int[] executeBatch() throws SQLException Submits

B/S中如果将客户端采集的指纹数据保存至数据库中

问题描述 我采用的指纹仪厂方未提供接口,于是我自已编写了一个客户端使用的windows控件库与服务器端接收数据的webservice客户端用户登录后我将用户ID放在session["id"]中客户端安装windows控件后,可以采集到指纹客户端网页中对象:<objectid="myfinger"classid="clsid:F4B78724-C517-4d6f-ABC2-06078541BAEA"width="600"h

使用JDBC在MySQL数据库中快速批量插入数据

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch() throws SQLExceptionAdds a set of parameters to this PreparedStatement object's batch of commands.int[] executeBatch() throws SQLExceptionSubmits a