从 CSV 文件读取

发布于 2024-10-02 06:35:40 字数 3122 浏览 0 评论 0原文

try {
    BufferedReader br = new BufferedReader(new InputStreamReader(item.getInputStream()));
    String strLine = "";
    StringTokenizer st = null;
    while ((strLine = br.readLine()) != null) {
        st = new StringTokenizer(strLine, "\t");
        while (st.hasMoreTokens()) {
            urlcnt = st.nextToken();
            srccnt = st.nextToken();
            contentType = st.nextToken();
            verticle = st.nextToken();
            timeFrame = st.nextToken();
        }
        if (con == null) {
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con = SQLConnection.getNewConnection();
            stmt = con.createStatement();
        }
        try {
            ResultSet rs;
            boolean hasRows = false;
            rs = stmt.executeQuery("select url from urls_temp where url='"+urlcnt+"'");
            while (rs.next()) {
                hasRows=true;
                i++;
            }
            if (!hasRows) {
                j++;
                PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                if (timeFrame.equalsIgnoreCase("Daily")) {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 0);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                    insertUrlStatement.close();
                } else {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 1);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                }
            }
        }
    }
}

上面的代码用于将详细信息上传到数据库,这些详细信息在 CSV 文件中以制表符分隔。

CSV 文件的示例格式如下,效果很好:

http://avb.com(tab space)asdf(tab space)asdf(tab space)asdd(tab space)asdf

http://anything.com(tab space)asdf(tab space)asdf(tab space)asdfasd(tab space)asdfsadf

有时我可能需要插入一些 null 值CSV 文件中的数据库如下:

http://asdf.com(tab space)(tab space)aasddf(tab space)(tab space)asdfsad

但这不起作用,什么也没有得到插入数据库。

为了在表的第二和第四 (srcnt & verticle) 列中插入 null 值,必须对上述程序进行哪些修改?

try {
    BufferedReader br = new BufferedReader(new InputStreamReader(item.getInputStream()));
    String strLine = "";
    StringTokenizer st = null;
    while ((strLine = br.readLine()) != null) {
        st = new StringTokenizer(strLine, "\t");
        while (st.hasMoreTokens()) {
            urlcnt = st.nextToken();
            srccnt = st.nextToken();
            contentType = st.nextToken();
            verticle = st.nextToken();
            timeFrame = st.nextToken();
        }
        if (con == null) {
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con = SQLConnection.getNewConnection();
            stmt = con.createStatement();
        }
        try {
            ResultSet rs;
            boolean hasRows = false;
            rs = stmt.executeQuery("select url from urls_temp where url='"+urlcnt+"'");
            while (rs.next()) {
                hasRows=true;
                i++;
            }
            if (!hasRows) {
                j++;
                PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                if (timeFrame.equalsIgnoreCase("Daily")) {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 0);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                    insertUrlStatement.close();
                } else {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 1);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                }
            }
        }
    }
}

The above code is used for uploading details to the database that are given tab separated in a CSV file.

Sample format of CSV file will be as follows and this works fine:

http://avb.com(tab space)asdf(tab space)asdf(tab space)asdd(tab space)asdf

http://anything.com(tab space)asdf(tab space)asdf(tab space)asdfasd(tab space)asdfsadf

Sometimes I may need some null values to be inserted into the database from the CSV file as follows:

http://asdf.com(tab space)(tab space)aasddf(tab space)(tab space)asdfsad

But this is not working, nothing is getting inserted into the database.

What modification has to be done to the above program for inserting null values in the second and fourth (srccnt & verticle) columns of the table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-10-09 06:35:40

StringTokenizer 将连续分隔符视为单个分隔符。您说输入包含 [制表符空格] 作为分隔符,但代码的其余部分似乎并不期望空格,因此,如果没有更多信息,我会猜测输入仅用制表符分隔(而不是 [制表符] space]),并且相邻的分隔符被忽略,然后最后一个 nextToken() 抛出一个您忽略的异常。

这里的答案是使用 split() 重写它,如 Javadoc

StringTokenizer 是一个遗留类
出于兼容性原因保留
尽管在新版本中不鼓励使用它
代码。建议任何人
寻求此功能使用
String 的 split 方法或
改为 java.util.regex 包。

也就是说,您应该查看任何现有的 CSV 库(Google for Java CSV)。

StringTokenizer treats consecutive delimiters as a single delimiter. You say the input contains [tab space] as delimiters, but the rest of your code doesn't seem to be expecting spaces so, without more information, I'm going to guess that the input is delimited only with tabs (not [tab space]), and the adjacent delimiters are being ignored, Then the last nextToken() throws an exception which you are ignoring.

The answer here is to rewrite this using split(), as recommended in the Javadoc

StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.

That said, you should look at any of the existing CSV libraries out there (Google for Java CSV).

把回忆走一遍 2024-10-09 06:35:40

我建议您调试您的代码,以更具体地找出它的行为不符合您预期的地方,然后,如有必要,发布更具体的问题。

I would advise you to debug your code to figure out more specifically where it's not behaving as you expect and then, if necessary, post a more specific question.

感受沵的脚步 2024-10-09 06:35:40

我使用 FileHelpers 来解析 csv 文件。 http://www.filehelpers.com/。它读取 csv 文件并给我一个对象输出,这就是我想要的。而且,如果我传递对象列表,它会为我创建一个 csv 文件。给它一个机会。

I use FileHelpers for parsing csv files. http://www.filehelpers.com/. It reads a csv file and gives me an output in object, which is what I desire. And also if I pass the list of objects it would create a csv file for me. Give it a chance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文