Home Database Mysql Tutorial 通过数据库数据构建树结构(一)

通过数据库数据构建树结构(一)

Jun 07, 2016 pm 03:41 PM
introduce how us Tutorial data database structure pass

在本教程中,我们将向我们介绍如何通过数据库中的数据动态地构建树结构。我们将使用 NetBeans IDE 6.0 构建一个由两个页面组成的应用程序,其中第一页包含一个 JSF 1.2 ( Woodstock ) 树组件。接下来,我们将用数据库中的人物姓名填充树结构的一级节点,然

在本教程中,我们将向我们介绍如何通过数据库中的数据动态地构建树结构。我们将使用NetBeans IDE 6.0构建一个由两个页面组成的应用程序,其中第一页包含一个JSF 1.2Woodstock树组件。接下来,我们将用数据库中的人物姓名填充树结构的一级节点,然后用此人的行程信息来填充二级节点。TRIP节点将链接到第二个页面,其中显示该行程的详细信息。

目录

设计主页

连接数据库

通过数据库表构建树结构

添加详细信息页

添加代码

定义页面导航

更多功能:将Action方法与树节点绑定

关于树节点选择的注意事项



    本教程将涉及以下技术和资源

JavaServer Faces组件/
Java EE
平台

1.2 with Java EE 5*
1.1 with J2EE 1.4

Travel数据库

必需

    要利用NetBeans IDE 6.0Java EE 5的性能,我们需要一个与Java EE 5规范完全兼容的应用服务器,比如说Sun Java System Application Server 9GlassFish项目)。

    本教程适用于GlassFish V2应用服务器。如果我们使用的是其他服务器,请参阅发行说明 常见问题解答 了解各类问题和解决途径。有关所支持的服务器和Java EE平台的详细信息,请参阅发行说明

设计主页

    首先,我们将构建一个包含树组件和TRIP数据库表的主页。

 

  1. 创建一个新的Visual Web JSF应用程序项目,将其命名为DatabaseTree,然后启动Viual Web JSF框架。

  2. 从组件面板的Basic部分拖动一个树组件到页面上,键入“Travel信息”,然后按回车键。在“属性”窗口中,将id属性设置为displayTree,将clientSide属性设置为True

clientSide属性为True时,每个子节点(无论展开与否)都将发送给客户机,但它们只有在父节点展开时才可见。当clientSideFalse时,仅呈现那些展开的父节点的子节点。

  1. 选择Tree Node 1,单击鼠标右键,然后从弹出菜单中选择“删除”。

在本应用程序中,我们将通过编程填充树中的节点,因此不需要初始化由IDE创建的树节点。如果未移除该节点,则JSP标记属性中设置的值将优先于运行时设置,并且页面将显示节点。

  1. 从组件面板拖动一个“消息组”组件到页面的边缘位置,如页面的右上角。

连接数据库

    接下来,将该页与Travel数据源中的数据库表相连接,然后使用查询编辑器修改用于检索数据的SQL查询,使游客的姓名按字母顺序显示,旅行日期按时间顺序显示。

  1. 打开“服务”窗口,展开数据库节点,然后验证是否已连接到Travel数据库。

如果Travel数据库的jdbc节点标记显示为断开,并且无法展开该节点,则表明IDE未连接到该数据库。要连接Travel数据库,请右键单击Travel数据库的jdbc节点,然后从弹出菜单中选择“连接”。如果出现“连接”对话框,在输入travel作为口令,选中“在此期间记住密码”,然后单击“确定”按钮。

注:如果我们使用的是Apache Tomcat应用服务器,请将derbyClient.jar文件复制到/common/lib目录,然后再尝试连接到该数据库。

  1. 展开Travel数据库的jdbc节点,然后展开“表”节点

     

 

  1. 将“TRIP”节点拖放到可视编辑器中。

导航窗口净土在页面1部分显示“tripDataProvider”节点,在“SessionBean1”部分显示“tripRowSet”节点。

  1. 在导航窗口中,展开“SessionBean1”节点,右键单击“tripRowSet”节点,然后选择“编辑SQL语句”。

在编辑区域将显示带有TRIP表格图的查询编辑器。

  1. 从“服务”窗口拖出“行程 > > 人员”节点,并将其放置在查询编辑器中的“TRIP”表图的旁边,如图3所示。

此时将出现另一个表图,且两个表图之间有链接或连接。

  1. 在“PERSON”表中,取消选中PERSONID复选框。

  2. 在查询编辑器的“设计网格”中,找到“TRAVEL.PERSON”表中的“NAME”行。单击“排序类型”单元格,然后从下拉列表中选择“升序”。

此操作将使数据库表中的名字按姓氏的字母顺序排列。

  1. 找到TRAVEL.TRIP表中的DEPDATE行。单击“排序类型”单元格,然后从下拉列表中选择“升序”。

此操作将行程日期按照从早到晚的顺序排列。

 

通过数据库表构建树结构

    现在,我们已经在存储停息中添加了一个请求bean属性,可供应用程序的中两个页面使用。然后,我将在<span>prerender()</span>方法中添加代码,用于通过TRIPPERSON数据库表动态地构建树组件。

  1. 打开页面1,使导航窗口可见。在导航窗口中,右键单击RequestBean1节点,然后选择“编辑Java源代码”。

  2. 在“public class RequestBean1 extends AbstractRequestBean”构建函数中声明属性,如下所示:

<font><span>private Integer personId;</span><span><p></p></span></font>
Copy after login
  1. Java编辑器中单击鼠标右键,选择“重构 > 封装”字段。

  2. 在“封装字段”对话框中,选择创建gettersetter方法,如下图所示。确保变量声明中字段可见性为“私有”,存取器可见性是“公有”,然后单击“重构”按钮。

 

  1. Java编辑器中打开页面1然后找到<span><font>prerender</font></span>方法。用以下粗体显示的代码替换<span>prerender</span><span>方法的主体部分</span><span>:</span>

代码示例1:页面1<span>prerender</span>方法

<span><font><span>    </span>public void prerender() {<strong><p></p></strong></font></span>
Copy after login
<strong><span><font><span>        </span>// If the Request Bean's personId is set, then<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>// we just came back from the Trip page<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>// and had displayed a selected trip.<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>// We use the personId later to determine whether<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>// to expand a person's node<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>Integer expandedPersonId = getRequestBean1().getPersonId();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>        </span>try {<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>// Set up the variables we will need<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>Integer currentPersonId = new Integer(-1);<p></p></font></span></strong>
Copy after login
<strong><span><font><span>         </span><span>   </span>// If nbrChildren is not 0 then this is a<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>// postback and we have our tree already<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>int nbrChildren = displayTree.getChildCount();<p></p></font></span></strong>
Copy after login
<strong><span><p><font> </font></p></span></strong>
Copy after login
Copy after login
Copy after login
<strong><span><font><span>            </span>if (nbrChildren == 0) {<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// List of outer (person) nodes<p></p></font></span></strong>
Copy after login
<strong><span><font><span>              </span><span>  </span>List outerChildren = displayTree.getChildren();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// Erase previous contents<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>outerChildren.clear();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// List of inner (trip) nodes<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>List innerChildren = null;<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// Execute the SQL query<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>tripDataProvider.refresh();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// Iterate over the rows of the result set.<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// Every time we encounter a new person, add first level node.<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>// Add second level trip nodes to the parent person node.<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>boolean hasNext = tripDataProvider.cursorFirst();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>while (hasNext) {<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>Integer newPersonId =<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                            </span>(Integer) tripDataProvider.getValue(<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                            </span>"TRIP.PERSONID");<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>if (!newPersonId.equals(currentPersonId)) {<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>currentPersonId = newPersonId;<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>TreeNode personNode = new TreeNode();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>personNode.setId("person" + newPersonId.toString());<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>personNode.setText(<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                                </span>(String)tripDataProvider.getValue(<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                                </span>"PERSON.NAME"));<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>// If the request bean passed a person id,<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                       </span><span> </span>// expand that person's node<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>personNode.setExpanded(newPersonId.equals<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                                </span>(expandedPersonId));<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>outerChildren.add(personNode);<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                        </span>innerChildren = personNode.getChildren();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>}<p></p></font></span></strong>
Copy after login
<strong><span><p><font> </font></p></span></strong>
Copy after login
Copy after login
Copy after login
<strong><span><font><span>                    </span>// Create a new trip node<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>TreeNode tripNode = new TreeNode();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>tripNode.setId("trip" +<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                            </span>tripDataProvider.getValue("TRIP.TRIPID").toString());<p></p></font></span></strong>
Copy after login
Copy after login
<strong><span><font><span>                    </span>tripNode.setText(<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                            </span>tripDataProvider.getValue("TRIP.DEPDATE").toString());<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>tripNode.setUrl("/faces/Trip.jsp?tripId=" +<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                            </span>tripDataProvider.getValue("TRIP.TRIPID").toString());<p></p></font></span></strong>
Copy after login
Copy after login
<strong><span><font><span>                    </span>innerChildren.add(tripNode);<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                    </span>hasNext = tripDataProvider.cursorNext();<p></p></font></span></strong>
Copy after login
<strong><span><font><span>                </span>}<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>}<p></p></font></span></strong>
Copy after login
<strong><span><p><font> </font></p></span></strong>
Copy after login
Copy after login
Copy after login
<strong><span><font><span>        </span>} catch (Exception ex) {<p></p></font></span></strong>
Copy after login
<strong><span><font><span>            </span>log("Exception gathering tree data", ex);<p></p></font></span></strong>
Copy after login
<strong><span><font><span>      </span><span>      </span>error("Exception gathering tree data: " + ex);<p></p></font></span></strong>
Copy after login
<font><strong><span><span>        </span>} </span></strong><span><p></p></span></font>
Copy after login
<span><font><span>     </span>}<p></p></font></span>
Copy after login
<span><font><span>               </span><p></p></font></span>
Copy after login


   
此代码读取按照personId排序的行程记录。对于每个personId,此代码会在树结构中创建一个新的一级节点。然后,为每一个与该personId关联的行程创建一个二级节点(嵌套节点)。最后,将二级行程节点与<span>tripNode_action</span>方法(稍后在本部分中创建)绑定在一起。

  1. 在源代码中单击鼠标右键,然后从弹出菜单中选择Fix Imports修复“无法找到类”的错误。在Fix All Imports对话框中,请确保<span>java.util.List</span>出现在列出的字段中,然后单击确定”按钮。

  2. 运行项目。

 Web浏览器将打开并显示一个树组件,其中每个一级节点显示人名,如下图所示。展开节点可显示此人的旅行日期。请注意,人名按姓氏的字母顺序显示,日期按时间顺序显示。在下一节中,我们将添加一些代码,以便用户在单击Trip节点时导航至第二页。第二个页面将显示用户所选行程的详细信息。

 

添加详细信息页

    现在,我们需要为应用程序添加第二个页面,如下图所示。此页使用“属性表单”组件动态地显示用户在第一页上所选行程的详细信息。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

See all articles