The simplest solution for Delphi and Java Web to read table data: tkbmmemtable + Java Data Format

Source: Internet
Author: User
In the era of Wide Web application, the C/S structure is not only based on the traditional database port connection method. If the network proxy and firewall are separated, Port Data is easily blocked. What we need is a network client application that can be accessed by Internet Explorer.

Here, my client uses Delphi

However, a problem arises here. When the data queried by Java is a list, how can we conveniently import the data to the table display on the Delphi client? Different people have different solutions. Some people use servlet to return XML for analysis. Some use servlet to generate a single string and then use split. Some use their own formatting analysis program and then fill in stringgrid.

I chose a simpler method: tkbmmemtable component + xfire. Xfire can easily transmit data by using HTTP, while tkbmmemtable can store data to tdataset for convenient use and access. If txquery is added. Wow .... The data processing and display functions become more powerful.

First, let's take a look at how convenient it is to pass a data table:
1. Define fields in the table to be queried in tkbmmemtable, and set active to true. The formatting component uses tkbmcsvstreamformat, which is set by default.
2. Use the list data queried by Dao in Java and use memtablerecbuilder (described later) to create CSV data that can be read by tkbmmemtable.

 

3. Pass resultarray to the client. Here I use my own set of universal WebService interfaces for data retrieval, which is not the focus of this Article. Therefore, ignore it. Because different people have different data transmission solutions.
4. Add the obtained data to the memory table through stringstream

Procedure tformrecordserver. bsskinbuttonsbarsections0items0click (
Sender: tobject );
VaR
Strstream: tstringstream;
Begin

Kbmmemtableprjinf. emptytable;
Strstream: = tstringstream. Create (datautil. findrecordingprjinf); // read data string
If strstream. size> 0 then
Kbmmemtableprjinf. loadfromstream (strstream); // this way, all data is transmitted to tkbmmemtable.
Strstream. Free;
End;

There is no redundant code. Except for xfire's own data transmission method, encoding and reading are just as simple as the above Code. Next, I will publish my own tkbmmemtable CSV data formatter:

Package com. aherp. Voice. util;

Public class memtablerecbuilder ...{

Private Static final string csv_qoute = """;

Private Static final char csv_record_delimiter = ',';

Private Static final string csv_true_string = "true ";

Private Static final string csv_false_string = "false ";

Private stringbuffer strbuf;

Private threadlocal <integer> fieldcount = new threadlocal <integer> ();

Public memtablerecbuilder ()...{
Strbuf = new stringbuffer ();
Fieldcount. Set (0 );
}

Public memtablerecbuilder buildfield (Object fielddata )...{
Int fieldcountint = fieldcount. Get ();
If (fieldcountint> 0)
Strbuf. append (csv_record_delimiter );
Fieldcount. Set (fieldcountint + 1 );

Strbuf. append (csv_qoute );
If (fielddata instanceof string )...{
Strbuf. append (
(String) fielddata). Replace (csv_qoute ,"""")
. Replace ("%", "% ")
. Replace ("", "% C ")
. Replace ("", "% N "));

}
Else if (fielddata instanceof Boolean )...{
If (Boolean) fielddata)
Strbuf. append (csv_true_string );
Else
Strbuf. append (csv_false_string );
}
Else
Strbuf. append (fielddata );
Strbuf. append (csv_qoute );
Return this;
}

Public memtablerecbuilder newrow ()...{
Strbuf. append ('');
Fieldcount. Set (0 );
Return this;
}

Public String tostring ()...{
Return strbuf. tostring ();
}
}

Note that for bool data storage, kbmmemtable is determined by the two attributes of its formatter csvstreamformat csvfalsestring and csvtruestring.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.