把文本文件的數(shù)據(jù)批量導入到數(shù)據(jù)庫中,是搜來的例子。本來想借用一下,可是因為中文問題和讀文本數(shù)據(jù)的問題,最后放棄了。
1、將數(shù)據(jù)按一定規(guī)律錄入到一個文本文件,每一行代表一條記錄。
下面是數(shù)據(jù)庫建表SQL:
CREATE TABLE t_FltPsgInfo -- 航班乘客信息
(
FltNum VARCHAR(10), -- 航班號
FltLine VARCHAR(30), -- 航線
FltDate VARCHAR(10), -- 日期
PsgName VARCHAR(30), -- 姓名
PsgType VARCHAR(30), -- 乘客類型,數(shù)字表示,目前是1-13
PsgSex VARCHAR(1), -- 0 男 1 女
PsgCab VARCHAR(1), -- 幾等艙, F/Y 艙位按字母順序排列
PsgSeatNo VARCHAR(5),-- 座位號 2A,22F,根據(jù)這個得到一排有多少個座位,共有多少排座位信息
PsgInfo VARCHAR(2048) -- 詳細信息,可能很長
)
我們將向表t_FltPsgInfo中插入1000條記錄。
新建一個文本文件,每一行代表一條記錄,如:
HU7804,廣州-北京,2007-07-18,謝麗珍,3,1,C,3A,服務保障信息:未用餐隨行人員…
其中以“,”作為字段的分隔標志,我們在解析這個文本文件時將根據(jù)“,”來拆分字段值。
按照上面的格式,將要插入的數(shù)據(jù)輸入到文本文件中,注意,是每一行代表一條記錄,或者你已有從數(shù)據(jù)庫導出的文本文件,那你就只需找到文件的規(guī)律,稍作調整就行了。
2、編寫Java源碼
1》數(shù)據(jù)庫操作類InsertDB.java
1
package test;
2
3
import java.sql.Connection;
4
5
import java.sql.DriverManager;
6
7
import java.sql.ResultSet;
8
9
import java.sql.Statement;
10
11
public class InsertDB
{
12
13
private static final String user = "sa";
14
15
private static final String pwd = "sa";
16
17
private static final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hhfly";
18
19
private static final String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
20
21
public static Connection getCon()
{
22
23
Connection con = null;
24
25
try
{
26
27
Class.forName(driver).newInstance();
28
29
con = DriverManager.getConnection(url, user, pwd);
30
31
if (con != null)
{
32
33
System.out.println("你已連接到數(shù)據(jù)庫:" + con.getCatalog());
34
35
}
36
37
} catch (Exception e)
{
38
39
System.out.println("連接數(shù)據(jù)庫失敗!");
40
41
e.printStackTrace();
42
43
}
44
45
return con;
46
47
}
48
49
public boolean insertDB(String FltNum, String FltLine, String FltDate,
50
51
String PsgName, String PsgType, String PsgSex, String PsgCab,
52
53
String PsgSeatNo, String PsgInfo)
{
54
55
Connection con = null;
56
57
Statement stm = null;
58
59
boolean flag = false;
60
61
String sql = "insert into t_FltPsgInfo values('" + FltNum + "','"
62
63
+ FltLine + "','" + FltDate + "','" + PsgName + "','" + PsgType
64
65
+ "','" + PsgSex + "','" + PsgCab + "','" + PsgSeatNo + "','"
66
67
+ PsgInfo + "')";
68
69
try
{
70
71
con = getCon();
72
73
stm = con.createStatement();
74
75
int i = stm.executeUpdate(sql);
76
77
if (i > 0)
{
78
79
flag = true;
80
81
System.out.println(flag + "插入數(shù)據(jù)成功!");
82
83
}
84
85
} catch (Exception e)
{
86
87
flag = false;
88
89
e.printStackTrace();
90
91
} finally
{
92
93
close(null, stm, con);
94
95
}
96
97
return flag;
98
99
}
100
101
//關閉相關連接
102
103
public void close(ResultSet rs, Statement stm, Connection con)
{
104
105
if (rs != null)
106
107
try
{
108
109
rs.close();
110
111
} catch (Exception e)
{
112
113
e.printStackTrace();
114
115
}
116
117
if (stm != null)
118
119
try
{
120
121
stm.close();
122
123
} catch (Exception e)
{
124
125
e.printStackTrace();
126
127
}
128
129
if (con != null)
130
131
try
{
132
133
con.close();
134
135
} catch (Exception e)
{
136
137
e.printStackTrace();
138
139
}
140
141
}
142
143
}
144
145
146
147
148
2》數(shù)據(jù)采集類DataGather.java
149
150
package test;
151
152
import java.io.RandomAccessFile;
153
154
import java.io.UnsupportedEncodingException;
155
156
public class DataGather
{
157
158
private static final String path = "src/resource/test";
159
160
public static final String openFileStyle = "r";
161
162
public static final String fieldLimitChar = ",";
163
164
public static final int fieldAllCount = 9;
165
166
private int count;
167
168
private String FltNum;
169
170
private String FltLine;
171
172
private String FltDate;
173
174
private String PsgName;
175
176
private String PsgType;
177
178
private String PsgSex;
179
180
private String PsgCab;
181
182
private String PsgSeatNo;
183
184
private String PsgInfo;
185
186
/**//*
187
188
* 功能:解析文本文件
189
190
*/
191
192
public void loadFile()
{
193
194
try
{
195
196
RandomAccessFile raf = new RandomAccessFile(path, openFileStyle);
197
198
String line_record = raf.readLine();
199
200
while (line_record != null)
{
201
202
// 解析每一條記錄
203
204
parseRecord(line_record);
205
206
line_record = raf.readLine();
207
208
}
209
210
System.out.println("共有合法的記錄" + count + "條");
211
212
} catch (Exception e)
{
213
214
e.printStackTrace();
215
216
}
217
218
}
219
220
221
222
/**//*
223
224
* 功能:具體解析每一條記錄,這里可以增加很多對記錄的解析判斷條件,如是否為字母、
225
226
* 數(shù)字、email等。
227
228
*/
229
230
private void parseRecord(String line_record) throws Exception
{
231
232
//拆分記錄
233
234
String[] fields = line_record.split(fieldLimitChar);
235
236
if (fields.length == fieldAllCount)
{
237
238
FltNum = tranStr(fields[0]);
239
240
FltLine = tranStr(fields[1]);
241
242
FltDate = tranStr(fields[2]);
243
244
PsgName = tranStr(fields[3]);
245
246
PsgType = tranStr(fields[4]);
247
248
PsgSex = tranStr(fields[5]);
249
250
PsgCab = tranStr(fields[6]);
251
252
PsgSeatNo = tranStr(fields[7]);
253
254
PsgInfo = tranStr(fields[8]);
255
256
System.out.println(FltNum + " " + FltLine + " " + FltDate + " "
257
258
+ PsgName + " " + PsgType + " " + PsgSex + " " + PsgCab
259
260
+ " " + PsgSeatNo + " " + PsgInfo);
261
262
InsertDB db = new InsertDB();
263
264
db.insertDB(FltNum, FltLine, FltDate, PsgName, PsgType, PsgSex,
265
266
PsgCab, PsgSeatNo, PsgInfo);
267
268
count++;
269
270
}
271
272
}
273
274
275
276
private String tranStr(String oldstr)
{
277
278
String newstr = "";
279
280
try
{
281
282
newstr = new String(oldstr.getBytes("ISO-8859-1"), "GBK");
283
284
} catch (UnsupportedEncodingException e)
{
285
286
e.printStackTrace();
287
288
}
289
290
return newstr;
291
292
}
293
294
}
295
296
297
298
299
3》測試類Test.java
300
301
package test;
302
303
304
305
public class Test
{
306
307
public static void main(String[] args)
{
308
309
try
{
310
311
DataGather gather = new DataGather ();
312
313
gather.loadFile();
314
315
} catch (Exception e)
{
316
317
e.printStackTrace();
318
319
}
320
321
}
322
323
}
324