001.
package
com.java.myapp;
002.
003.
import
java.awt.EventQueue;
004.
import
java.awt.event.ActionEvent;
005.
import
java.awt.event.ActionListener;
006.
import
java.beans.PropertyChangeEvent;
007.
import
java.beans.PropertyChangeListener;
008.
import
java.io.BufferedReader;
009.
import
java.io.File;
010.
import
java.io.FileReader;
011.
import
java.io.IOException;
012.
import
java.sql.Connection;
013.
import
java.sql.DriverManager;
014.
import
java.sql.SQLException;
015.
import
java.sql.Statement;
016.
import
java.util.ArrayList;
017.
018.
import
javax.swing.JButton;
019.
import
javax.swing.JFileChooser;
020.
import
javax.swing.JFrame;
021.
import
javax.swing.JLabel;
022.
import
javax.swing.JOptionPane;
023.
import
javax.swing.JProgressBar;
024.
import
javax.swing.SwingWorker;
025.
import
javax.swing.JTextField;
026.
027.
public
class
MyForm
extends
JFrame {
028.
029.
private
JProgressBar progressBar;
030.
private
JButton btnStart;
031.
private
JTextField txtFile;
032.
033.
public
static
void
main(String[] args) {
034.
EventQueue.invokeLater(
new
Runnable() {
035.
public
void
run() {
036.
MyForm form =
new
MyForm();
037.
form.setVisible(
true
);
038.
}
039.
});
040.
}
041.
042.
public
MyForm() {
043.
044.
045.
super
(
"ThaiCreate.Com Java GUI Tutorial"
);
046.
setSize(
525
,
270
);
047.
setLocation(
500
,
280
);
048.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
049.
getContentPane().setLayout(
null
);
050.
051.
052.
final
JLabel lblTitle =
new
JLabel(
"Import Data"
, JLabel.CENTER);
053.
lblTitle.setBounds(
61
,
24
,
370
,
14
);
054.
getContentPane().add(lblTitle);
055.
056.
057.
progressBar =
new
JProgressBar();
058.
progressBar.setStringPainted(
true
);
059.
progressBar.setMinimum(
0
);
060.
progressBar.setMaximum(
100
);
061.
progressBar.setBounds(
162
,
98
,
190
,
20
);
062.
getContentPane().add(progressBar);
063.
064.
065.
066.
btnStart =
new
JButton(
"Start"
);
067.
btnStart.addActionListener(
new
ActionListener() {
068.
public
void
actionPerformed(ActionEvent e) {
069.
btnStart.setEnabled(
false
);
070.
EventQueue.invokeLater(
new
Runnable() {
071.
@Override
072.
public
void
run() {
073.
new
BackgroundWorker().execute();
074.
}
075.
});
076.
077.
}
078.
});
079.
btnStart.setBounds(
209
,
144
,
100
,
23
);
080.
getContentPane().add(btnStart);
081.
082.
083.
JLabel lblFileName =
new
JLabel(
"File Name"
);
084.
lblFileName.setBounds(
99
,
70
,
57
,
14
);
085.
getContentPane().add(lblFileName);
086.
087.
088.
txtFile =
new
JTextField();
089.
txtFile.setBounds(
162
,
67
,
182
,
20
);
090.
getContentPane().add(txtFile);
091.
txtFile.setColumns(
10
);
092.
093.
094.
JButton btnChoose =
new
JButton(
"..."
);
095.
btnChoose.addActionListener(
new
ActionListener() {
096.
public
void
actionPerformed(ActionEvent e) {
097.
JFileChooser fileopen =
new
JFileChooser();
098.
int
ret = fileopen.showDialog(
null
,
"Choose file"
);
099.
if
(ret == JFileChooser.APPROVE_OPTION) {
100.
101.
txtFile.setText(fileopen.getSelectedFile().toString());
102.
}
103.
}
104.
});
105.
btnChoose.setBounds(
354
,
66
,
26
,
23
);
106.
getContentPane().add(btnChoose);
107.
108.
}
109.
110.
public
class
BackgroundWorker
extends
SwingWorker<Void, Void> {
111.
112.
public
BackgroundWorker() {
113.
addPropertyChangeListener(
new
PropertyChangeListener() {
114.
@Override
115.
public
void
propertyChange(PropertyChangeEvent evt) {
116.
progressBar.setValue(getProgress());
117.
}
118.
119.
});
120.
}
121.
122.
@Override
123.
protected
void
done() {
124.
125.
JOptionPane.showMessageDialog(
null
,
126.
"Import Data Successfully"
);
127.
btnStart.setEnabled(
true
);
128.
}
129.
130.
protected
Void doInBackground()
throws
Exception {
131.
132.
133.
File file =
new
File(txtFile.getText());
134.
ArrayList<String> myArrList =
new
ArrayList<String>();
135.
try
{
136.
BufferedReader br =
new
BufferedReader(
new
FileReader(file));
137.
String line;
138.
while
((line = br.readLine()) !=
null
) {
139.
myArrList.add(line);
140.
}
141.
br.close();
142.
}
catch
(IOException e) {
143.
144.
e.printStackTrace();
145.
}
146.
147.
148.
Connection connect =
null
;
149.
Statement s =
null
;
150.
151.
try
{
152.
Class.forName(
"com.mysql.jdbc.Driver"
);
153.
connect = DriverManager.getConnection(
""
155.
+
"?user=root&password=root"
);
156.
s = connect.createStatement();
157.
158.
159.
for
(
int
i =
0
; i < myArrList.size(); i++) {
160.
161.
String sql =
"INSERT INTO data "
162.
+
"(DataLine) "
163.
+
"VALUES ('"
+ myArrList.get(i) +
"') "
;
164.
s.execute(sql);
165.
setProgress((
int
)((i*
100
)/myArrList.size())+
1
);
166.
Thread.sleep(
10
);
167.
}
168.
169.
170.
}
catch
(Exception e) {
171.
172.
e.printStackTrace();
173.
}
174.
175.
try
{
176.
if
(s !=
null
) {
177.
s.close();
178.
connect.close();
179.
}
180.
}
catch
(SQLException e) {
181.
182.
System.out.println(e.getMessage());
183.
e.printStackTrace();
184.
}
185.
186.
return
null
;
187.
}
188.
189.
}
190.
191.
}