开发者

Reading from a CSV file

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?


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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜