FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/DbfToHsqlToCsv/src/gui/EnterPoint.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
DbfToHsqlToCsv
/
src
/
gui
/
EnterPoint.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
328 lines (299 loc) · 11.5 KB
Breadcrumbs
java-code-example
/
DbfToHsqlToCsv
/
src
/
gui
/
EnterPoint.java
Copy path
File metadata and controls
executable file
·
328 lines (299 loc) · 11.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package
gui
;
import
java
.
awt
.
BorderLayout
;
import
javax
.
swing
.*;
import
javax
.
swing
.
filechooser
.
FileFilter
;
import
table
.
FieldTableModel
;
import
table
.
FieldTablePanel
;
import
com
.
linuxense
.
javadbf
.
DBFReader
;
import
common
.
Field
;
import
database
.
Connector
;
import
database
.
HsqldbConnection
;
import
java
.
awt
.
event
.*;
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
nio
.
charset
.
Charset
;
import
java
.
sql
.
Connection
;
import
java
.
sql
.
PreparedStatement
;
import
java
.
sql
.
ResultSet
;
import
com
.
csvreader
.
CsvWriter
;
public
class
EnterPoint
extends
JFrame
{
private
final
static
long
serialVersionUID
=
1L
;
public
static
void
main
(
String
[]
args
) {
new
EnterPoint
();
}
private
JTextField
pathToDbf
;
private
JTextField
pathToCsv
;
/** ïîëÿ èç ôàéëà DBF */
private
Field
[]
fields
;
/** êîìïîíåíò îòîáðàæåíèÿ ïîëåé DBF â âèäå ñïèñêà */
private
FieldTablePanel
table
;
private
JButton
buttonLoad
;
private
JButton
buttonConvert
;
private
JCheckBox
checkBoxUnique
;
/** ãëàâíàÿ ôîðìà, êîòîðàÿ îòîáðàæàåò èíòåðôåéñ ïîëüçîâàòåëÿ */
public
EnterPoint
() {
super
(
"DBF to CSV converter"
);
initComponents
();
this
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
this
.
setSize
(
300
,
500
);
this
.
setVisible
(
true
);
}
private
void
initComponents
() {
// ñîçäàòü êîìîïîíåíòû
pathToDbf
=
new
JTextField
(
"D:
\\
temp
\\
viddil_js.dbf "
);
pathToCsv
=
new
JTextField
();
JButton
buttonPathToDbf
=
new
JButton
(
"..."
);
JButton
buttonPathToCsv
=
new
JButton
(
"..."
);
buttonLoad
=
new
JButton
(
"Load"
);
buttonConvert
=
new
JButton
(
"Convert"
);
checkBoxUnique
=
new
JCheckBox
(
"Distinct"
,
false
);
JPanel
panelDbf
=
getBorderPanelWithTitle
(
"Ïóòü ê DBF äëÿ ïîëó÷åíèÿ äàííûõ"
,
buttonPathToDbf
,
null
,
null
,
null
,
pathToDbf
);
JPanel
panelCsv
=
getBorderPanelWithTitle
(
"Ïóòü ê CSV äëÿ âûâîäà ðåçóëüòàòà "
,
buttonPathToCsv
,
null
,
null
,
null
,
pathToCsv
);
this
.
table
=
new
FieldTablePanel
();
// íàçíà÷èòü ñëóøàòåëåé
buttonPathToDbf
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
onPathToDbf
();
}
});
buttonPathToCsv
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
onPathToCsv
();
}
});
buttonLoad
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
onButtonLoad
();
}
});
buttonConvert
.
addActionListener
(
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
onButtonConvert
();
}
});
// ðàññòàâèòü êîìïîíåíòû
JPanel
panelMain
=
new
JPanel
();
GroupLayout
groupLayout
=
new
GroupLayout
(
panelMain
);
panelMain
.
setLayout
(
groupLayout
);
GroupLayout
.
SequentialGroup
groupLayoutHorizontal
=
groupLayout
.
createSequentialGroup
();
GroupLayout
.
SequentialGroup
groupLayoutVertical
=
groupLayout
.
createSequentialGroup
();
groupLayout
.
setHorizontalGroup
(
groupLayoutHorizontal
);
groupLayout
.
setVerticalGroup
(
groupLayoutVertical
);
groupLayoutHorizontal
.
addGroup
(
groupLayout
.
createParallelGroup
()
.
addComponent
(
panelDbf
).
addComponent
(
panelCsv
).
addComponent
(
buttonLoad
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
this
.
table
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
Short
.
MAX_VALUE
)
.
addGroup
(
groupLayout
.
createSequentialGroup
()
.
addComponent
(
buttonConvert
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
checkBoxUnique
)
)
);
groupLayoutVertical
.
addComponent
(
panelDbf
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
);
groupLayoutVertical
.
addComponent
(
panelCsv
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
);
groupLayoutVertical
.
addComponent
(
buttonLoad
);
groupLayoutVertical
.
addComponent
(
this
.
table
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
PREFERRED_SIZE
,
Short
.
MAX_VALUE
);
groupLayoutVertical
.
addGroup
(
groupLayout
.
createParallelGroup
()
.
addComponent
(
buttonConvert
)
.
addComponent
(
checkBoxUnique
)
);
this
.
getContentPane
().
add
(
panelMain
);
}
private
void
onPathToDbf
() {
JFileChooser
fileChooser
=
new
JFileChooser
();
fileChooser
.
setFileFilter
(
new
ExtendFileFilter
(
"DBF"
));
fileChooser
.
setCurrentDirectory
(
new
File
(
System
.
getProperty
(
"user.dir"
)));
if
(
fileChooser
.
showDialog
(
this
,
"DBF äëÿ ÷òåíèÿ"
) ==
JFileChooser
.
APPROVE_OPTION
) {
this
.
pathToDbf
.
setText
(
fileChooser
.
getSelectedFile
()
.
getAbsolutePath
());
}
else
{
// user click CancelButton
}
}
private
void
onPathToCsv
() {
JFileChooser
fileChooser
=
new
JFileChooser
();
fileChooser
.
setFileFilter
(
new
ExtendFileFilter
(
"CSV"
));
fileChooser
.
setCurrentDirectory
(
new
File
(
System
.
getProperty
(
"user.dir"
)));
if
(
fileChooser
.
showSaveDialog
(
this
) ==
JFileChooser
.
APPROVE_OPTION
){
if
(
fileChooser
.
getSelectedFile
().
getAbsolutePath
().
toUpperCase
().
indexOf
(
".CSV"
)<
0
){
this
.
pathToCsv
.
setText
(
fileChooser
.
getSelectedFile
()
.
getAbsolutePath
()+
".csv"
);
}
else
{
this
.
pathToCsv
.
setText
(
fileChooser
.
getSelectedFile
()
.
getAbsolutePath
());
}
}
else
{
// user click CancelButton
}
}
/** reaction on Striking ButtonConvert */
private
void
onButtonConvert
(){
Connection
connection
=
null
;
try
{
// çàãðóçèòü äàííûå èç ôàéëà â áàçó äàííûõ HSQL
System
.
out
.
println
(
"get Connection"
);
Connector
connector
=
new
HsqldbConnection
(
"temp_data"
);
connection
=
connector
.
getConnection
();
String
tableName
=(
new
File
(
this
.
pathToDbf
.
getText
())).
getName
();
tableName
=
tableName
.
substring
(
0
,
tableName
.
indexOf
(
'.'
));
System
.
out
.
println
(
"remove table by DBF filename"
);
try
{
connection
.
createStatement
().
executeUpdate
(
"DROP TABLE "
+
tableName
);
connection
.
commit
();
System
.
out
.
println
(
"table was dropped"
);
}
catch
(
Exception
ex
){
System
.
out
.
println
(
"DropTable "
+
tableName
+
"Exception:"
+
ex
.
getMessage
());
}
System
.
out
.
println
(
"create table by fieldNames and DBF filename"
);
String
sql
=
null
;
try
{
sql
=
Field
.
getSqlCreateTable
(
tableName
,
fields
);
connection
.
createStatement
().
executeUpdate
(
sql
.
toString
());
connection
.
commit
();
}
catch
(
Exception
ex
){
// SQL çàïðîñ íå âûïîëíåí - ïðè÷èíà - ex.getMessage()
System
.
out
.
println
(
"createTable - query is not execute:"
+
ex
.
getMessage
()+
"
\n
"
+
sql
);
}
System
.
out
.
println
(
"put data into Connection"
);
PreparedStatement
statement
=
Field
.
getPreparedStatement
(
connection
,
tableName
,
fields
);
DBFReader
reader
=
new
DBFReader
(
new
FileInputStream
(
this
.
pathToDbf
.
getText
()));
Object
[]
row
;
while
( (
row
=
reader
.
nextRecord
())!=
null
){
statement
.
clearParameters
();
for
(
int
counter
=
0
;
counter
<
fields
.
length
;
counter
++){
if
(
row
[
counter
]
instanceof
String
){
//logger.debug(counter+" : "+Field.convertDosString((String)fields[counter].getObjectForSql(row[counter])));
statement
.
setObject
(
counter
+
1
,
Field
.
convertDosString
((
String
)
fields
[
counter
].
getObjectForSql
(
row
[
counter
])));
}
else
{
if
(
row
[
counter
]!=
null
){
//logger.debug(counter+":"+row[counter].getClass().getName());
statement
.
setObject
(
counter
+
1
,
fields
[
counter
].
getObjectForSql
(
row
[
counter
]));
}
else
{
statement
.
setObject
(
counter
+
1
,
fields
[
counter
].
getObjectForSql
(
row
[
counter
]));
}
}
}
try
{
statement
.
executeUpdate
();
connection
.
commit
();
}
catch
(
Exception
ex
){
System
.
err
.
println
(
"Execute Exception: "
+
ex
.
getMessage
());
}
}
System
.
out
.
println
(
"Execute query by Order "
);
String
queryMain
=
this
.
table
.
getSqlQueryByFieldsPosition
(
tableName
,
null
,
this
.
checkBoxUnique
.
isSelected
());
System
.
out
.
print
(
"Query:"
);
System
.
out
.
println
(
queryMain
);
ResultSet
rs
=
connection
.
createStatement
().
executeQuery
(
queryMain
);
CsvWriter
writer
=
new
CsvWriter
(
this
.
pathToCsv
.
getText
(),
','
,
Charset
.
forName
(
"WINDOWS-1251"
));
int
columnSelectedCount
=
this
.
table
.
getFieldCount
();
String
[]
headers
=
new
String
[
columnSelectedCount
];
for
(
int
counter
=
0
;
counter
<
headers
.
length
;
counter
++){
headers
[
counter
]=
this
.
table
.
getSelectedFieldName
(
counter
);
}
writer
.
writeRecord
(
headers
);
while
(
rs
.
next
()){
writer
.
writeRecord
(
getArrayOfStringByResultSet
(
rs
,
headers
));
}
writer
.
close
();
JOptionPane
.
showMessageDialog
(
this
,
"OK"
);
this
.
pathToDbf
.
setEnabled
(
true
);
this
.
pathToCsv
.
setEnabled
(
true
);
this
.
buttonLoad
.
setEnabled
(
true
);
this
.
buttonConvert
.
setEnabled
(
true
);
}
catch
(
Exception
ex
){
JOptionPane
.
showMessageDialog
(
this
,
ex
.
getMessage
(),
"Error"
,
JOptionPane
.
ERROR_MESSAGE
);
}
finally
{
try
{
connection
.
close
();
}
catch
(
Exception
ex
){};
}
}
private
String
[]
getArrayOfStringByResultSet
(
ResultSet
rs
,
String
[]
columns
){
String
[]
returnValue
=
new
String
[
columns
.
length
];
try
{
for
(
int
counter
=
0
;
counter
<
columns
.
length
;
counter
++){
returnValue
[
counter
]=
rs
.
getString
(
columns
[
counter
]);
}
}
catch
(
Exception
ex
){
System
.
err
.
println
(
"getArrayOfStringByResultSet:"
+
ex
.
getMessage
());
};
return
returnValue
;
}
/** reaction on Striking ButtonLoad */
private
void
onButtonLoad
() {
try
{
// çàãðóçèòü âñå ïîëÿ èç ôàéëà DBF â âèçóàëüíûé êîìïîíåíò
DBFReader
reader
=
new
DBFReader
(
new
FileInputStream
(
this
.
pathToDbf
.
getText
()));
fields
=
new
Field
[
reader
.
getFieldCount
()];
for
(
int
counter
=
0
;
counter
<
fields
.
length
;
counter
++) {
fields
[
counter
] =
new
Field
(
reader
.
getField
(
counter
),
counter
);
}
this
.
table
.
setTableModel
(
new
FieldTableModel
(
fields
));
this
.
pathToDbf
.
setEnabled
(
false
);
this
.
buttonLoad
.
setEnabled
(
false
);
this
.
buttonConvert
.
setEnabled
(
true
);
}
catch
(
Exception
ex
) {
JOptionPane
.
showMessageDialog
(
this
,
"Load Error:"
+
ex
.
getMessage
());
}
}
/**
* ïîëó÷èòü ïàíåëü ñ êîìïîíåíòàìè íà áîðòó, êîòîðàÿ îáðàìëåíà òåêñòîì è
* äîáàâèòü êîìïîíåíòû â ýòó ïàíåëü EAST, SOUTH, WEST, NORTH, CENTRE
*/
private
JPanel
getBorderPanelWithTitle
(
String
title
,
JComponent
...
components
) {
JPanel
panel
=
new
JPanel
(
new
BorderLayout
());
panel
.
setBorder
(
javax
.
swing
.
BorderFactory
.
createTitledBorder
(
title
));
String
[]
position
=
new
String
[] {
BorderLayout
.
EAST
,
BorderLayout
.
SOUTH
,
BorderLayout
.
WEST
,
BorderLayout
.
NORTH
,
BorderLayout
.
CENTER
};
for
(
int
counter
=
0
;
counter
<
position
.
length
;
counter
++) {
if
(
components
.
length
>
counter
) {
if
(
components
[
counter
] !=
null
) {
panel
.
add
(
components
[
counter
],
position
[
counter
]);
}
}
}
return
panel
;
}
}
class
ExtendFileFilter
extends
FileFilter
{
String
[]
fileExt
;
public
ExtendFileFilter
(
String
...
ext
) {
this
.
fileExt
=
ext
;
}
@
Override
public
boolean
accept
(
File
f
) {
if
(
f
.
isDirectory
()) {
return
true
;
}
else
{
String
name
=
f
.
getName
();
int
dotPosition
=
name
.
lastIndexOf
(
'.'
);
String
ext
=
name
.
substring
(
dotPosition
+
1
);
return
elementInArray
(
ext
);
}
}
private
boolean
elementInArray
(
String
value
) {
boolean
returnValue
=
false
;
for
(
int
counter
=
0
;
counter
<
this
.
fileExt
.
length
;
counter
++) {
if
(
this
.
fileExt
[
counter
].
equalsIgnoreCase
(
value
)) {
returnValue
=
true
;
break
;
}
}
return
returnValue
;
}
@
Override
public
String
getDescription
() {
StringBuffer
description
=
new
StringBuffer
();
if
(
fileExt
!=
null
) {
for
(
int
counter
=
0
;
counter
<
fileExt
.
length
;
counter
++) {
description
.
append
(
this
.
fileExt
[
counter
]);
if
(
counter
!= (
fileExt
.
length
-
1
)) {
description
.
append
(
", "
);
}
}
}
return
description
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL