lillian1205

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 5 Stories :: 0 Comments :: 0 Trackbacks
把文本文件的数据批量导入到数据库中,是搜来的例子。本来想借用一下,可是因为中文问题和读文本数据的问题,最后放弃了。


1、将数据按一定规律录入到一个文本文件,每一行代表一条记录。

下面是数据库建表SQL:
CREATE TABLE t_FltPsgInfo  -- 航班乘客信息

(

    FltNum  VARCHAR(10), -- 航班号

    FltLine  VARCHAR(30),  -- 航线

    FltDate  VARCHAR(10),  -- 日期

    PsgName  VARCHAR(30),  -- 姓名

    PsgType  VARCHAR(30), -- 乘客类型,数字表示,目前是1-13

    PsgSex  VARCHAR(1),  -- 0 男  1 女

    PsgCab  VARCHAR(1),  -- 几等舱, F/Y  舱位按字母顺序排列

    PsgSeatNo  VARCHAR(5),-- 座位号 2A,22F,根据这个得到一排有多少个座位,共有多少排座位信息

    PsgInfo  VARCHAR(2048) -- 详细信息,可能很长

)

我们将向表t_FltPsgInfo中插入1000条记录。

新建一个文本文件,每一行代表一条记录,如:

HU7804,广州-北京,2007-07-18,谢丽珍,3,1,C,3A,服务保障信息:未用餐随行人员…

其中以“,”作为字段的分隔标志,我们在解析这个文本文件时将根据“,”来拆分字段值。

按照上面的格式,将要插入的数据输入到文本文件中,注意,是每一行代表一条记录,或者你已有从数据库导出的文本文件,那你就只需找到文件的规律,稍作调整就行了。

2、编写Java源码

1》数据库操作类InsertDB.java

  1package test; 
  2
  3import java.sql.Connection; 
  4
  5import java.sql.DriverManager; 
  6
  7import java.sql.ResultSet; 
  8
  9import java.sql.Statement; 
 10
 11public 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("你已连接到数据库:" + con.getCatalog()); 
 34
 35            }
 
 36
 37        }
 catch (Exception e) 
 38
 39            System.out.println("连接数据库失败!"); 
 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 + "插入数据成功!"); 
 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
1482》数据采集类DataGather.java 
149
150package test; 
151
152import java.io.RandomAccessFile; 
153
154import java.io.UnsupportedEncodingException; 
155
156public 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* 数字、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
2993》测试类Test.java 
300
301package test; 
302
303
304
305public 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
posted on 2009-09-29 14:45 lillian 阅读(467) 评论(0)  编辑  收藏 所属分类: java操作数据库数据

只有注册用户登录后才能发表评论。


网站导航: