读取txt文件生成数据

InsertDB.java

package org.jeecg.modules.to.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

//数据库操作类InsertDB.java
public class InsertDB {

    private static final String user = "AutoTEST";

    private static final String pwd = "Dhl12345";

    private static final String url = "jdbc:sqlserver://CNSHAWS20007:1525;DatabaseName=AutoTEST";

    private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static Connection getCon() {

        Connection con = null;

        try {

            Class.forName(driver).newInstance();

            con = DriverManager.getConnection(url, user, pwd);

            if (con != null) {

                System.out.println("你已连接到数据库:" + con.getCatalog());

            }

        } catch (Exception e) {

            System.out.println("连接数据库失败!");

            e.printStackTrace();

        }

        return con;

    }

    public boolean insertDB(String StaffNo, String Status, String EnglishName,
                            String ChineseName, String Email, String BU,
                            String Department, String Location, String ReportTo,
                            String Title, String Station, String Office,
                            String FltNuma, String FltNumb, String FltNumc,
                            String FltNumw, String JoinDatq, String StaffType,
                            String FltNume, String LeaveDate, String FltDater,
                            String PsgNamet, String PsgNamey, String PsgNameu,
                            String PsgNamej, String PsgNamem,String PsgNamedg
                            ) {

        Connection con = null;

        Statement stm = null;

        boolean flag = false;

        String sql = "insert into to_FltPsgInfo values('" + StaffNo + "','"
                + Status + "','"
                + EnglishName + "','"
                + ChineseName + "','"
                + Email + "','"
                + BU + "','"
                + Department + "','"
                + Location + "','"
                + ReportTo + "','"
                + Title + "','"
                + Station + "','"
                + Office + "','"
                + FltNuma + "','"
                + FltNumb + "','"
                + FltNumc + "','"
                + FltNumw + "','"
                + JoinDatq + "','"
                + StaffType + "','"
                + FltNume + "','"
                + LeaveDate + "','"
                + FltDater + "','"
                + PsgNamet + "','"
                + PsgNamey + "','"
                + PsgNameu + "','"
                + PsgNamej + "','"
                + PsgNamem + "','"
                + PsgNamedg + "')";
        try {

            con = getCon();

            stm = con.createStatement();

            int i = stm.executeUpdate(sql);

            if (i > 0) {

                flag = true;

                System.out.println(flag + "插入数据成功!");

            }

        } catch (Exception e) {

            flag = false;

            e.printStackTrace();

        } finally {

            close(null, stm, con);

        }

        return flag;

    }

    //关闭相关连接

    public void close(ResultSet rs, Statement stm, Connection con) {

        if (rs != null)

            try {

                rs.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        if (stm != null)

            try {

                stm.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        if (con != null)

            try {

                con.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

    }

}

DataGather.java

package org.jeecg.modules.to.util;

import java.io.RandomAccessFile;

import java.io.UnsupportedEncodingException;

//数据采集类DataGather.java
public class DataGather {

    //private static final String path = "src/resource/test";
    public static final String path = "C:\\Users\\oshait24\\Desktop\\users.txt";
    public static final String openFileStyle = "r";

    public static final String fieldLimitChar = ";";

    public static final int fieldAllCount = 22;

    public static final String encoding = "utf8";

    private int count;

    private String StaffNo;

    private String Status;

    private String EnglishName;

    private String ChineseName;

    private String Email;

    private String BU;

    private String Department;

    private String Location;

    private String ReportTo;

    private String Title;

    private String Station;

    private String Office;

    private String FltNuma;

    private String FltNumb;

    private String FltNumc;

    private String FltNumw;

    private String JoinDatq;

    private String StaffType;

    private String FltNume;

    private String LeaveDate;

    private String FltDater;

    private String PsgNamet;

    private String PsgNamey;

    private String PsgNameu;

    private String PsgNamej;

    private String PsgNamem;

    private String PsgNamedg;

    /*

     * 功能:解析文本文件

     */

    public void loadFile() {

        try {

            RandomAccessFile raf = new RandomAccessFile(path, openFileStyle);
            //String s = new String(raf.readLine().getBytes("iso8859-1"), "utf-8");
            //System.out.println("中文处理" + s);
            String line_record = raf.readLine();

            while (line_record != null) {

                // 解析每一条记录

                parseRecord(line_record);

                line_record = raf.readLine();

            }

            System.out.println("共有合法的记录" + count + "条");

        } catch (Exception e) {

            e.printStackTrace();

        }

    }



    /*

     * 功能:具体解析每一条记录,这里可以增加很多对记录的解析判断条件,如是否为字母、

     * 数字、email等。

     */

    private void parseRecord(String line_record) throws Exception {

        //拆分记录

        String[] fields = line_record.split(fieldLimitChar);

        if (fields.length == fieldAllCount) {

            StaffNo = tranStr(fields[0]);

//            if (tranStr(fields[1])==("Loff")){
//                Status = "2";
//            }else if (tranStr(fields[1])==("Norm")){
//                Status = "1";
//            }
           Status = tranStr(fields[1]);

            EnglishName = tranStr(fields[2]);

            ChineseName = tranStr(fields[3]);

            Email = tranStr(fields[4]);

            BU = tranStr(fields[5]);

            Department = tranStr(fields[6]);

            Location = tranStr(fields[7]);

            ReportTo = tranStr(fields[8]);

            Title = tranStr(fields[9]);

            Station = tranStr(fields[10]);

            Office = tranStr(fields[11]);

            FltNuma = tranStr(fields[12]);

            FltNumb = tranStr(fields[13]);

            FltNumc = tranStr(fields[14]);

            FltNumw = tranStr(fields[15]);

            JoinDatq = tranStr(fields[16]);

            StaffType = tranStr(fields[17]);

            FltNume = tranStr(fields[18]);

            LeaveDate = tranStr(fields[19]);

            FltDater = tranStr(fields[20]);

            PsgNamet = tranStr(fields[21]);

            System.out.println(StaffNo + " "
                    + Status
                    + " " + EnglishName
                    + " " + ChineseName
                    + " " + Email
                    + " " + BU
                    + " " + Department
                    + " " + Location
                    + " " + ReportTo
                    + " " + Title
                    + " " + Station
                    + " " + Office
                    + " " + FltNuma
                    + " " + FltNumb
                    + " " + FltNumc
                    + " " + FltNumw
                    + " " + JoinDatq
                    + " " + StaffType
                    + " " + FltNume
                    + " " + LeaveDate
                    + " " + FltDater
                    + " " + PsgNamet);

            InsertDB db = new InsertDB();

            db.insertDB(StaffNo, Status, EnglishName, ChineseName, Email, BU,
                    Department, Location, ReportTo, Title, Station, Office, FltNuma, FltNumb
                    , FltNumc, FltNumw, JoinDatq, StaffType, FltNume
                    , LeaveDate, FltDater, PsgNamet, PsgNamey, PsgNameu, PsgNamej, PsgNamem, PsgNamedg);
            count++;

        }

    }


    private String tranStr(String oldstr) {

        String newstr = "";

        try {

            newstr = new String(oldstr.getBytes("ISO-8859-1"), "GBK");

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        }

        return newstr;

    }

}


TextUtil.java

package org.jeecg.modules.to.util;

//测试类Test.java
public class TextUtil {

    public static void main(String[] args) {

        try {

            DataGather gather = new DataGather();

            gather.loadFile();

        } catch (Exception e) {

            e.printStackTrace();

        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值