-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountFile.java
More file actions
149 lines (129 loc) · 4.3 KB
/
AccountFile.java
File metadata and controls
149 lines (129 loc) · 4.3 KB
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
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class AccountFile {
private File openFile;
private static JFrame frame;
private static JPanel panel;
private static JButton button_back;
private static JButton button_import;
private static JLabel successlabel;
// MySQL 8.0 以上版本 - JDBC 驅動名稱及數據庫 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 數據庫的自己的用户名跟密碼
static final String USER = "root";
static final String PASS = "Nknu@123456";
public void setAccountFile(File openFile) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(openFile.toPath().toString()));// 檔案讀取路徑
BufferedReader reader = new BufferedReader(isr);
String line = null;
try {
while ((line = reader.readLine()) != null) {
String item[] = line.split(",");
String data1 = item[0].trim();
String data2 = item[1].trim();
sql = "INSERT INTO `runoob`.`account` (`account`, `password`) VALUES ('" + data1 + "', '"
+ data2 + "')";
stmt.executeUpdate(sql);
System.out.print(data1 + "\t" + data2 + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
public AccountFile() {
panel = new JPanel();
panel.setBackground(SystemColor.WHITE);
frame = new JFrame("AccountFile!");
frame.setBounds(50, 50, 720, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
successlabel = new JLabel("");
successlabel.setBounds(10, 50, 120, 25);
panel.add(successlabel);
button_import = new JButton("匯入資料");
button_import.setBounds(10, 20, 120, 25);
button_import.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showDialog(new JLabel(), "選擇");
openFile = jfc.getSelectedFile();
if (openFile.isDirectory()) {
System.out.println("資料夾:" + openFile.getAbsolutePath()); // 尋找要匯入的csv檔
} else if (openFile.isFile()) {
System.out.println("檔案:" + openFile.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
setAccountFile(openFile);
successlabel.setText("Update Successful!");
}
});
panel.add(button_import);
button_back = new JButton("返回上一頁");
button_back.setBounds(500, 20, 120, 25);
button_back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
panel.add(button_back);
frame.setVisible(true);
}
}