最佳实践数据库访问 - 联系人列表 [英] Best practice Database Access - list of contacts

查看:133
本文介绍了最佳实践数据库访问 - 联系人列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道很长时间,同时为这个问题创建几个愚蠢的解决方案,如何正确地完成以下操作:

i have been wondering for a long time, whilst creating several stupid solutions for this Problem, how the following should be done properly:

DataTable:
ID | givenname | lastname |地址| data ...

DataTable: ID | givenname | lastname | address | data ...

我想显示列表框中的所有givennames + lastnames,并且选择一个项目,我想检索相应数据集的其余部分,在一些文本框中。

I want to show all givennames + lastnames in a listbox and uppon selection of one item, i want to retrieve the rest of the corresponding dataset and display is in some textboxes.

所以我基本上做:

1)查询:SELECT ID,givenname,lastname FROM DataTable;

1) Query: SELECT ID, givenname, lastname FROM DataTable;

2)创建和联系对象的ArrayList(只有一个类与DataTable具有相同的成员列),我迭代结果集,使用ID,givenname和lastname创建几个这些对象。

2) Create and ArrayList of "Contact Objects" (just a class that has the same members as the DataTable has columns), I iterate over the resultset and so create several of these Objects with ID, givenname and lastname.

!3)我想让列表框显示givenname ++ lastname, 我想让程序知道这个john Doe的数组列表的哪个元素(如果有多个),从对象中退出ID并退出他的地址和data

!3) i want the listbox to display givenname + " " + lastname, but on selection of "John Doe" i want the programm to know what element of the arraylist this "john Doe" was (in case there is more than one of them), retireve the ID from the object and retireve his address and "data"

我经常做的是像第三个John Doe在列表框中被选中,让我们在数组列表中查找第三个john doe,并希望上帝这是正确的
这对我来说是非常不必要的。

What i often did was something like "the 3rd John Doe on the listbox was selected, lets look for the 3rd john doe in the arraylist and hope to god that this is right" which seems highly unnecessary to me.

在Java + Swing中,这是一个可用的解决方案。

What is a usable solution for this in Java + Swing

我可以以某种方式清楚我需要什么^^

i hope i could somehow make clear what i need^^

关于
Billdoor

regards Billdoor

推荐答案

要显示对象,Swing组件将使用对象放置在其中的toString()方法。一个常见的方法是创建一个包含名称,ID等的Data类,实现toString()以显示所需的内容,然后在JList中放置这些对象的列表。然后在选择时,获取所选项,将其转换到数据类,然后调用getID()。

To display an Object, Swing components will use the toString() method the Object placed in it. A common approach is to create a Data class that holds the name, ID, etc., implement toString() to display what you want, and then put a list of these objects in your JList. Then on selection, get the selected item, cast it to the data class, and then call getID().

这篇关于最佳实践数据库访问 - 联系人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆