-1

我有一个包含四行的文本文件,每行包含逗号分隔的值,如下面的文件

我的文件是:

    Raj,raj34@myown.com,123455
   kumar,kumar@myown.com,23453
   shilpa,shilpa@myown.com,765468
   suraj,suraj@myown.com,876567

我有一个包含四个字段的 MySQL 表

  firstname    lastname      email                phno
  ----------  ----------    ---------            --------
   Raj           babu      raj34@hisown.com       2343245
   kumar         selva     kumar@myown.com        23453
   shilpa        murali    shilpa@myown.com       765468
   suraj         abd       suraj@myown.com        876567

现在我想通过 Java 使用上述文本文件中的数据更新我的表。

我尝试使用 bufferedReader 从文件中读取数据,并使用逗号作为分隔符的拆分方法并将其存储在数组中。但它不起作用。任何帮助表示赞赏。

这是我迄今为止尝试过的

void readingFile()
    {
        try
        {
            File f1 = new File("TestFile.txt");
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);
            String strln = null;
                        strln = br.readLine();

            while((strln=br.readLine())!=null)
            {
//              System.out.println(strln);
                                 arr = strln.split(",");
                                 strfirstname = arr[0];
                                 strlastname = arr[1];
                                stremail = arr[2];
                                strphno = arr[3];
                                System.out.println(strfirstname + " " + strlastname + " " + stremail +" "+ strphno);
                        } 

//                        for(String i : arr)
//                        {

//                        }


            br.close();
            fr.close();
        }
        catch(IOException e)
        {
            System.out.println("Cannot read from File." + e);
        }

        try
           {
                st = conn.createStatement();
                String query = "update sampledb set email = stremail,phno =strphno where  firstname = strfirstname ";
                st.executeUpdate(query);
                st.close();
                System.out.println("sampledb Table successfully updated.");
            }
             catch(Exception e3)
            {
             System.out.println("Unable to Update sampledb table. " + e3);   
           }

    }

我得到的输出是:

Ganesh Pandiyan ganesh1@myown.com 9591982389
Dass Jeyan jeyandas@myown.com 9689523645
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Gowtham Selvan gowthams@myown.com 9894189423
    at TemporaryPackages.FileReadAndUpdateTable.readingFile(FileReadAndUpdateTable.java:35)
    at TemporaryPackages.FileReadAndUpdateTable.main(FileReadAndUpdateTable.java:72)
Java Result: 1

@varadaraj:这是你的代码....

String stremail,strphno,strfirstname,strlastname;
//    String[] arr;
    Connection conn;
    Statement st;
    void readingFile()
    {
        try {
             BufferedReader bReader= new BufferedReader(new FileReader("TestFile.txt"));
             String fileValues;

            while ((fileValues = bReader.readLine()) != null)
            {
                String[] values=fileValues .split(",");
                strfirstname = values[0];
//                strlastname = values[1];
                stremail = values[1];
                strphno = values[2];
                System.out.println(strfirstname + " " + strlastname + " " + stremail +" "+ strphno);
            }
        bReader.close();
    } catch (IOException e) {
        System.out.println("File Read Error");
    }
//                        for(String i : arr)
//                        {

//                        }



        try
           {
                st = conn.createStatement();
                String query = "update sampledb set email = stremail,phno =strphno where  firstname = strfirstname ";
                st.executeUpdate(query);
                st.close();
                System.out.println("sampledb Table successfully updated.");
            }
             catch(Exception e3)
            {
             System.out.println("Unable to Update sampledb table. " + e3);   
           }

    }
4

3 回答 3

0

您在尝试访问索引 1 时得到 ArrayIndexOutOfBoundException ,即在 lastname 字段值,因此请检查您的文本文件中的任何列表元素在索引 1 处是否没有数据

于 2013-10-29T08:20:23.400 回答
0

尝试这个

    public class FileReaderTesting {
    static String stremail;
    static String strphno;
    static String strfirstname;
    static String strlastname;
    static Connection conn;
    static Statement st;
    public static void main(String[] args) {


            try {
                 BufferedReader bReader= new BufferedReader(new FileReader("C:\\fileName.txt"));
                 String fileValues;

                while ((fileValues = bReader.readLine()) != null)
                {
                    String[] values=fileValues .split(",");
                    strfirstname = values[0];
//                  strlastname = values[1];
                    stremail = values[1];
                    strphno = values[2];
                    System.out.println(strfirstname + " " + stremail +" "+ strphno);

                        st = conn.createStatement();
                        String query = "update sampledb set email = '"+stremail+"',pno = '"+strphno+"' where  firstname = '"+strfirstname+"' ";
                        System.out.println(query);
                        st.executeUpdate(query);
                        st.close();
                        System.out.println("sampledb Table successfully updated.");
                }
            bReader.close();
        } catch (IOException e) {
            System.out.println("File Read Error");
        }
            catch(Exception e3)
            {
             System.out.println("Unable to Update sampledb table. " + e3);   
           }


    }
}
于 2013-10-29T07:41:59.957 回答
0

你所拥有的看起来像一个 CSV 文件,你可以考虑像Super CSV这样的库来帮助你阅读和解析文件。

于 2013-10-29T07:48:23.847 回答