本帖最后由 tianshan20081 于 2011-12-25 15:32 编辑
今天找到一的200多M的txt 文件。文件格式表规范。
代码:- public static void main(String[] args) throws IOException {
-
- BufferedReader br = new BufferedReader(new InputStreamReader(new
- FileInputStream("D:\\TDDownload\\XXX.sql")));
- String text ;
- int i = 0 ;
- System.out.println(new Timestamp(System.currentTimeMillis()));
- while((text = br.readLine())!= null){
- String[] ss= text.split("#");
- User u = new User();
- u.setUsername(ss[0].trim());
- u.setPassword(ss[1].trim());
- u.setEmail(ss[2].trim());
-
- UserDao ud = new UserDao();
- ud.addUser(u);
- i++ ;
-
- }
- System.out.println(i);
- System.out.println(new Timestamp(System.currentTimeMillis()));
- }
复制代码- public void addUser(User u){
- Connection conn = DBUtil.getconn();
- PreparedStatement ps = null ;
- String sql = "insert into tb_csdnuser(username,password,email) values(?,?,?)";
- try {
- // System.out.println(u.getUsername()+"#"+u.getPassword()+"#"+u.getEmail());
-
- ps = conn.prepareStatement(sql);
- ps.setString(1, u.getUsername());
- ps.setString(2, u.getPassword());
- ps.setString(3, u.getEmail());
- ps.execute();
-
- DBUtil.commit(conn);
- } catch (SQLException e) {
- DBUtil.rollback(conn);
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- DBUtil.closeConn(conn);
- DBUtil.closePS(ps);
- }
-
- }
复制代码 都写了好几个小时了 还没写完!
我的代码有哪些地方可以提高效率! |